简体   繁体   中英

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;

        proc.Start();

        proc.BeginErrorReadLine();
        proc.BeginOutputReadLine();

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

Run the command with

cmd.exe /c

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

So:

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

should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM