我无法理解 .NET 中 ProcessStartInfo 类的内部和外部。
我使用此类来执行 FFmpeg 等 .exe 程序,没有任何问题。
但是当我使用 ProcessStartInfo 启动 .cmd 程序时,例如一个简单的 foo.cmd 仅包含@echo Hello world
它不输出任何内容。
ProcessStartInfo oInfo = new ProcessStartInfo(@"C:\Program Files (x86)\itms\foo.cmd")
{
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
CreateNoWindow = true
};
using (Process p = new Process())
{
p.StartInfo = oInfo;
p.OutputDataReceived += new DataReceivedEventHandler(transporter_OutputDataReceived);
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();
}
private void transporter_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Response.Write(e.Data + " - line<br/>");
}
我见过很多例子,人们使用 cmd.exe 来启动 .cmd 程序,我已经尝试过这个,但没有成功。该程序只是无限期地加载。
ProcessStartInfo oInfo = new ProcessStartInfo("cmd", "/c start foo.cmd")
{
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
WorkingDirectory = @"C:\Program Files (x86)\itms"
};
在 Windows 和 Mac 上使用命令行工具时,foo.cmd 程序可以成功运行并输出。
有人可以帮我揭开这个神秘面纱吗?
Thanks
EDIT
该代码在本地执行时行为正确。当我在我们的网站上执行代码时出现问题。要么不允许程序执行,要么输出被某种方式禁用。
只有 cmd.exe 返回输出'“cmd”,“/c 目录”'例如返回有关当前文件夹内容的信息。
这实际上可能是一个权限问题吗?