A
A
Afrodita131242020-12-06 19:27:28
.NET
Afrodita13124, 2020-12-06 19:27:28

Can't find compiler executable csc.exe?

Visual Studio 2019 development environment, C# language, .NET Framework v4.7.2. I'm trying to compile some simple code that should compile a new file as the program runs. The code is taken from here - https://habr.com/ru/post/67431/ . An exception occurs while compiling

Cannot find compiler executable csc.exe
in line
CompilerResults results = provider.CompileAssemblyFromSource(compilerParams, source);
There are solutions to this problem on the net, but for other situations, but for me the error occurs in a simple code example.

Here is the whole code:

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.CSharp;

namespace ConsoleCompile
{
    class Program
    {
        static void Main(string[] args)
        {
            // Source code для компиляции
            string source =
            @"
using System.Collections.Generic;
using System.Linq;

namespace Foo
{
    public class Bar
    {
        public static void SayHello()
        {
            System.Console.WriteLine(""Hello World"");
            System.Console.WriteLine( string.Join("","", Enumerable.Range(0,10).Select(n=>n.ToString()).ToArray() ) );
        }
    }
}
            ";

            // Настройки компиляции 
            Dictionary<string, string> providerOptions = new Dictionary<string, string>
                {
                    {"CompilerVersion", "v4.7.2"}
                };
            CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);

            CompilerParameters compilerParams = new CompilerParameters
            { OutputAssembly = "D:\\Foo.EXE", GenerateExecutable = true };

            // Компиляция 
            CompilerResults results = provider.CompileAssemblyFromSource(compilerParams, source);

            // Выводим информацию об ошибках 
            Console.WriteLine("Number of Errors: {0}", results.Errors.Count);
            foreach (CompilerError err in results.Errors)
            {
                Console.WriteLine("ERROR {0}", err.ErrorText);
            }

            Console.ReadKey();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
rPman, 2020-12-06
@rPman

Just now in MVS Community 2017 your code was built without errors, didn't change or tweak anything.
Try to reinstall the .net framework, or rather update to the latest one, or specify repair in the installer

V
Vasily Bannikov, 2020-12-06
@vabka

You don't have csc in PATH. It must lie somewhere in the bowels of visual studio, emnip.
In general, there is no point in compiling code via csc now, when you can immediately access the api roslin.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question