Question

I got main.cs file in C:\kaannos\main.cs.

my string filepath:

this.filePath = @"c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe";

        startInfo.FileName = filePath;            
        startInfo.Arguments = "C:\\kaannos\\main.cs";            
        // startInfo.UseShellExecute = false;
        Process.Start(startInfo);

My console application shows up and right after comes csc.exe but it just pops up and close itself. I checked folder and there are no main.exe, so it didnt compile it. If I run this command from command prompt: csc C:\kaannos\main.cs it does work. I turned off my UAC so I dont need to run command prompt as administrator to test it, and also because I couldnt find a way to run process as administrator. But it seems like that is not the case because UAC is off? Any solution?

Was it helpful?

Solution

csc will always write the output into the current working directory unless you specify a different output location with the /out flag.

So it won't work if you don't have write access to the current working directory, unless you specify /out (or change the working directory via ProcessStartInfo.WorkingDirectory). If you're happy with it writing to the working directory, then that's where you should look for the result.

Additionally, as noted by Hans Passant in comments, you should consider other programmatic ways of generating code:

  • CSharpCodeProvider
  • The Roslyn API, which is currently in CTP, but which is the next-generation managed C# compiler, which will be the basis of Visual Studio in the future.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top