问题描述
我正在尝试在.aspx页上的C#代码中的命令提示符下执行命令.该代码不会引发任何错误,但是由于未生成新文件,因此该命令未执行.
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.
有什么想法为什么我的代码不能生成ResultadoCheckMac_100939.txt?
Any ideas why my code doesn't generate the ResultadoCheckMac_100939.txt?
代码:
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.");
调试视图输出:
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.
推荐答案
假设您要继续针对cmd提示执行它,您可以执行以下操作:
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
指示提示符执行以下字符串".但是,更干净的方法可能是拦截输出并自己捕获它:
/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();
}
}
示例
如果我想ping -n 1 8.8.8.8
,我可以使用以下任一方法来实现:
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);
或者,我可以这样做:
// 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
这篇关于在C#.aspx页面上通过重定向执行命令提示符中的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!