我整个上午都在搜索和试验这个,但我被难住了。我有一个在 IIS 中运行的 aspx 页面并调用以下 c# 函数。我试图让它运行一个 cmd 文件并返回 cmd 文件的输出。我在下面的代码中尝试了五个不同的选项:
protected String RunMyCmdFileAndGetResponse() {
Process proc = new Process ();
proc.StartInfo.FileName = @"c:\Windows\System32\cmd.exe";
// proc.StartInfo.Arguments = @"/c echo hello"; <== 1
// proc.StartInfo.Arguments = @"/c c:\mypath\myfile_badname.cmd"; <== 2
// proc.StartInfo.Arguments = @"/c type c:\mypath\myfile.cmd"; <== 3
proc.StartInfo.Arguments = @"/c c:\mypath\myfile.cmd"; // <== 4
// proc.StartInfo.Arguments = @"/c call c:\mypath\myfile.cmd"; <== 5
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string response = proc.StandardOutput.ReadToEnd();
response += proc.StandardError.ReadToEnd();
proc.WaitForExit();
return response;
}
Cmd文件c:\mypath\myfile.cmd内容为:
@echo test line 1 > c:\mypath\myfilelog.txt
@echo test line 2
此 cmd 文件在手动运行时按预期工作,生成 myfilelog.txt 并返回测试行 2。当使用 c# 代码执行时:
- 选项 1 有效 - 按预期返回“你好”响应。
- 选项 2 按预期失败,表明 myfile_badname.cmd 未被识别为有效命令
- 选项 3 按预期工作 - 它返回 myfile.cmd 的内容作为响应 - 这确认我能够找到并读取文件。
- 选项 4 行不通 - 据我所知,它应该行得通。它不挂断,也不返回任何响应,也不执行 cmd 文件(不生成 myfilelog.txt)。
- 选项 5 - 与选项 4 相同的结果。
注意 - 我还尝试修改 myfile.cmd 以删除第 1 行(创建日志文件)并仅保留第 2 行以回显响应。以防万一创建日志文件时出现权限问题。相同的结果。
如有任何帮助,我们将不胜感激!
已更新以添加解决方案:
@MaxOvrdrv 的回答让我有了不同的想法。在 UseShellExecute = false 的 IIS 上下文中运行 Process.Start 时确实存在某种限制 - 如果主要参数是可执行文件(cmd 文件、脚本文件等),它将不会运行它。我尝试将 SomeExample.cmd 传递给 cmd.exe,将 SomeExample.js 传递给 cscript.exe。
但是...我能够通过间接级别来欺骗它,这样可执行文件名就不再是第一个参数,这样它就可以正常工作。
运行cmd文件:
string theResponse = RunMyCmdAndGetResponse(@"c:\somepath\mycmd.cmd");
protected String RunMyCmdAndGetResponse(string cmdPath) {
Process proc = new Process ();
proc.StartInfo.FileName = @"c:\Windows\System32\cmd.exe";
proc.StartInfo.Arguments = "/c cmd /c " + cmdPath;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
string response = proc.StandardOutput.ReadToEnd();
response += proc.StandardError.ReadToEnd();
return response;
}
运行脚本文件:
string theResponse = RunMyScriptAndGetResponse(@"c:\somepath\myscript.js");
protected String RunMyScriptAndGetResponse(string scriptPath) {
Process proc = new Process ();
proc.StartInfo.FileName = @"c:\Windows\System32\cmd.exe";
proc.StartInfo.Arguments = "/c cscript //nologo " + scriptPath;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
string response = proc.StandardOutput.ReadToEnd();
response += proc.StandardError.ReadToEnd();
return response;
}
最佳答案
从 ASPX 页面运行批处理文件或任何进程都是徒劳的,因为 IIS 不在真正的 Windows 用户上下文下运行。因此,无论您为运行 AppPool 的用户提供了多少设置和权限,或者您进行了何种类型的配置更改,它都将永远无法运行。很久以前我遇到了同样的问题,基本上这是不可能的。
请在此处查看我之前的问题(和评论),以及针对您当前问题的可能“概念性”解决方案的已接受答案:
关于c# - Process.Start cmd.exe 不会运行在 IIS 中运行时作为参数传递的 cmd 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28590924/