I have a .NET program that launches cmd.exe to execute another program, "MyProgam.exe". I do this instead of launching MyProgram.exe because I need to run that as Administrator as well as capturing its output.
Here's what I have:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
string args =
@"/C ""C:\Program Files (x86)\MyFolder\MyProgram.exe"" ""arg1"" ""arg2"" > out.txt 2>&1";
startInfo.Arguments = args;
process.StartInfo = startInfo;
process.Start();
However, the output (as captured in output.txt) is:
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
So it appears as though the quotes surrounding C:\Program Files (x86)\MyFolder\MyProgram.exe are ignored. I have tried to wrap it in an extra pair of quotes, like this:
@"/C """"C:\Program Files (x86)\MyFolder\MyProgram.exe"""" ""arg1"" ""arg2"" > out.txt 2>&1";
which then produces the following:
The filename, directory name, or volume label syntax is incorrect.
What am I missing?