【问题标题】:Process.Start() not Executing my Command after the First TimeProcess.Start() 在第一次之后没有执行我的命令
【发布时间】:2015-10-30 16:13:25
【问题描述】:

我有以下代码启动 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”。

感谢您的宝贵时间。

【问题讨论】:

  • 您重定向了 StandardOutput 但您从未真正读取过它,也许这就是问题所在?还有,你为什么要启动 cmd.exe 而不是直接启动nsisExePath

标签: c# process cmd nsis


【解决方案1】:

makensis.exe 是一个控制台程序,使用 cmd.exe 作为中间人调用 makesis.exe 将一无所获。恰恰相反,通过将 cmd.exe 加入到组合中,您将接触到其 &/&&/|| 运算符和奇怪的引号处理。

我建议你直接启动makesis.exe。

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

  • /C 指示cmd.exe 执行命令然后退出
  • if 1==1 是一种减少奇怪引用处理的技巧

【讨论】:

  • 感谢安德斯,非常感谢。我不知道您需要解决“qoute 处理”错误。我不想直接调用 makensis.exe,因为这会导致 UAC 提示,然后我必须单击或以其他方式处理...
  • @Killercam:makensis.exe 不需要提升,您是否在 Windows 的兼容性选项卡上更改了某些内容?
  • 不,我没有。或者至少我不知道。当然,最初我确实是这样;直接启动 makensis.exe 然后我收到 UAC 警告...我现在将研究兼容性途径...非常感谢。
  • 谢谢,不知道之前发生了什么,但我已经改为直接使用 NSIS,一切都很好......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-14
  • 1970-01-01
  • 2018-07-17
  • 1970-01-01
  • 2013-07-05
  • 1970-01-01
相关资源
最近更新 更多