Process.Start()在第一次之后没有执行我的命令

时间:2015-10-30 16:13:25

标签: c# process cmd nsis

我有以下代码启动cmd.exe然后执行NSIS脚本的编译。

...
Process process = new System.Diagnostics.Process();
ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo()
{
    FileName = "cmd.exe",
    WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
    RedirectStandardInput = true,
    RedirectStandardOutput = true,
    UseShellExecute = false,
    CreateNoWindow = true,
};
process.StartInfo = startInfo;
if (!process.Start())
    throw new Exception("NSIS failed to start, find out why");

process.StandardInput.WriteLine(String.Format("\"{0}\" {1}", nsisExePath, packagePath));
process.StandardInput.WriteLine("exit");
//process.WaitForExit();

第一次执行代码时,这非常有效。但是,在同一会话中运行此进程的所有后续尝试都将失败。我必须重新启动我的应用程序才能让它再次运行。我已经注释掉WaitForExit()方法,因为这似乎导致进程挂起 - cmd.exe进程似乎永远不会进入exit命令。

如何让NSIS进程正常退出,或者如何在单个会话中多次使用它,我缺少什么?

我用来编译NSIS脚本的显式命令是“C:\ Program Files(x86)\ NSIS \ makensis.exe”。

感谢您的时间。

1 个答案:

答案 0 :(得分:2)

makensis.exe是一个控制台程序,使用cmd.exe作为中间人来调用makensis.exe,你什么都得不到。恰恰相反,通过将cmd.exe引入混合,您会看到& / && / ||运算符和奇怪的报价处理。

我建议您直接启动makensis.exe。

如果由于某种原因您认为必须使用cmd.exe,那么命令行应该类似于cmd.exe /C if 1==1 "c:\path\otherapp.exe" "c:\inputfile.ext"

  • /C指示cmd.exe执行命令然后退出
  • if 1==1是一种减少奇怪报价处理的黑客行为