MrFluffy MrFluffy - 1 year ago 186
C# Question

c# running command line with two arguments

so I'm currently working on a BlueJ like clone for c#. Now I want to compile all the .cs files in the working folder on click for which I use:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /define:DEBUG /optimize /out:Program.exe *.cs
which I took from the msdn page. For that to work though I need to be in the right directionary so I use the following:
cd /d + dir
where dir is the directionary of the files. Now when I try to run that from c# like this:

cmd = @"cd /d "+ dir + @" && C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /define:DEBUG /optimize /out:Program.exe *.cs";
file = "cmd";
var proc = new Process();
proc.StartInfo = new ProcessStartInfo(file, cmd);
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.EnableRaisingEvents = true;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
proc.ErrorDataReceived += proc_DataReceived;
proc.OutputDataReceived += proc_DataReceived;



nothing happenes. But when I try to run the command in the cmd window it works fine. Any ideas?

Answer Source

Run the command with

cmd.exe /c

cmd.exe expects parameter "/c" if you want to pass over an execution command.


cmd = @"/c cd /d "+ dir + @" && C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /define:DEBUG /optimize /out:Program.exe *.cs";

should work

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download