I'm attempting to execute a command in command prompt in C# code on a .aspx page. The code doesn't throw any errors, however, the command doesn't execute because a new file isn't generated.
I don't see any issues with the command itself because if I copy the command from debug view and paste it into command prompt, it executes fine.
Any ideas why my code doesn't generate the ResultadoCheckMac_100939.txt?
code:
string cmd = ejecutable_CheckMac + " " + archivo_temporal + " > " + archivo_resultado;
System.Diagnostics.Debugger.Log(0, null, "cmd: " + cmd);
System.Diagnostics.Debugger.Log(0, null, "Start cmd execution.");
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = cmd;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
System.Diagnostics.Debugger.Log(0, null, "Cmd execution finished.");
debug view output:
00000014 0.00096980 [136] cmd: C:\inetpub\wwwroot\cgi-bin\tbk_check_mac.exe C:\inetpub\wwwroot\cgi-bin\log\DatosParaCheckMac_100939.txt > C:\inetpub\wwwroot\cgi-bin\log\ResultadoCheckMac_100939.txt
00000015 0.00103170 [136] Start cmd execution.
00000016 0.01946740 [136] Cmd execution finished.
Assuming you want to continue to execute it against the cmd prompt, you can do:
var psi = new ProcessStartInfo("cmd", "/c " + cmd); // note the /c
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
Process.Start(psi);
/c
directs the prompt to "execute the following string". However, a cleaner method may be to intercept the output and catch it yourself:
var psi = new ProcessStartInfo(ejecutable_CheckMac, archivo_temporal);
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
using (var proc = Process.Start(psi))
{
using (StreamReader sr = proc.StandardOutput)
{
// This effectively becomes the intended contents
// of `archivo_resultado`
var summary = sr.ReadToEnd();
}
}
Example
If i wanted to ping -n 1 8.8.8.8
, I could do it using either:
// Execute ping against command prompt
var psi = new ProcessStartInfo("cmd", @"/c ping -n 1 8.8.8.8 > save\to\ping.txt");
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
Process.Start(psi);
Or, I could do:
// Call ping directly passing parameters and redirecting output
var psi = new ProcessStartInfo("ping", "-n 1 8.8.8.8");
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
using (var proc = Process.Start(psi))
{
using (StreamReader sr = proc.StandardOutput)
{
Console.WriteLine(sr.ReadToEnd());
}
}
Both end up giving me (roughly) the same output, it's just now I don't have to chase down a file.
Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8: bytes=32 time=18ms TTL=54
Ping statistics for 8.8.8.8:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 18ms, Maximum = 18ms, Average = 18ms