【发布时间】:2015-08-28 16:29:53
【问题描述】:
我正在尝试在 .aspx 页面上以 C# 代码的命令提示符执行命令。该代码不会引发任何错误,但是,该命令不会执行,因为没有生成新文件。
我没有看到命令本身有任何问题,因为如果我从调试视图复制命令并将其粘贴到命令提示符中,它可以正常执行。
任何想法为什么我的代码不生成 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 /c <cmd>或cmd /k <cmd>。您也可以针对ejecutable_CheckMac执行并将其用作FileName参数(然后在您的应用程序中重定向输出)。 -
IIS 用户是否对该目录有写权限?
-
另外,Brad 是对的,
>重定向支持由cmd.exe提供,CreateProcess不会自动提供它(这是Process.Start()在UseShellExecute = false时使用的)跨度> -
@BenVoigt 是的,读+写。
-
@BradChristie 抱歉,您的第一点是什么意思?我想要 cmd /c
而不是什么/在哪里?如果您提交答案,如果它解决了我的问题,我可以将其标记为正确。
标签: c# process cmd processstartinfo