1

I am trying to build a C# program that converts a different language into C# code. I have the program working fine, converting the code and writing it to a .cs file. I want to have this file automatically be compiled, and run, however I cannot figure out how to do this with C#.

I can do it manually by simply running a batch file I wrote, and I attempted to run this batch file from C# using the System.Diagnostics.Process class. When it ran it gave an error within the batch code itself, saying that none of the commands were found (the usual "not an executable, batch file etc"). I can't figure out why it runs normally, but not when ran from C#.

Here's the batch file code: C:\Program_Files_(x86)\Microsoft_Visual_Studio 10.0\VC\bin\amd64\vcvars64.bat csc %1.cs pause

and the function that calls it:

System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "compiler\\compile.bat";
process.StartInfo.Arguments = " "+fileName;
process.Start();
process.WaitForExit();
process.StartInfo.FileName = fileName + ".exe";
process.Start();
process.WaitForExit();
Console.WriteLine("done");

Any help would be greatly appreciated.

1

2 Answers 2

7

Don't call the C# compiler or any compiler of .net plataform using a batch script - it's a bad pratice. You can do this using only C#. using the CodeDomProvider class you can write this easily.

  static void CompileCSharp(string code) {
    CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");
    ICodeCompiler compiler = provider.CreateCompiler();
    CompilerParameters parameters = new CompilerParameters();
    parameters.OutputAssembly = @"D:\foo.exe";
    parameters.GenerateExecutable = true;
    CompilerResults results = compiler.CompileAssemblyFromSource(parameters, code);
    if (results.Output.Count == 0)
    {
        Console.WriteLine("success!");
    }
    else
    {
        CompilerErrorCollection CErros = results.Errors;
        foreach (CompilerError err in CErros)
        {
            string msg = string.Format("Erro:{0} on line{1} file name:{2}", err.Line, err.ErrorText, err.FileName);
            Console.WriteLine(msg);
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

CompileAssemblyFromSource call by CompilerParameters and string[],also from files by CompileAssemblyFromFile
Thank you very much for this. I remember coming across codedom before, but I forgot about it. Just out of curiosity why is it bad practice? I know in this particular case the codedom is better, but what if this wasn't written with .NET? Would the command line be fine, or no? Would I have to use dlls then.
use dlls set to parameters. ex)parameters.ReferencedAssemblies.Add("System.dll");
I think he meant it's a bad practice because there are native methods to do so.
ICodeCompiler compiler = provider.CreateCompiler(); is now deprecated. See: msdn.microsoft.com/en-us/library/…
0

You can programmatically compile your code inside of C# using the CSharpCodeProvider class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.