我想在 .NET 服务器端代码中执行程序。
到目前为止我有这个:
Process p = new Process();
p.StartInfo.FileName = "myProgram.exe";
p.StartInfo.Arguments = " < parameter list here > ";
p.Start();
p.Close();
这是一个控制台程序。发生的情况是控制台不断地打开和关闭。
最佳答案
你想要,
Process p = new Process();
p.StartInfo.FileName = "myProgram.exe";
p.StartInfo.Arguments = " < parameter list here > ";
p.Start();
p.WaitForExit();
您的代码中发生的情况是您启动该进程并立即关闭它, 你需要的是调用 WaitForExit() 它实际上等待进程自行关闭,
要在应用程序关闭之前打印输出:
Process p = new Process();
p.StartInfo.FileName = "myProgram.exe";
p.StartInfo.Arguments = " < parameter list here > ";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.WaitForExit();
Console.WriteLine(p.StandardOutput.ReadToEnd());
关于.net - 以编程方式运行可执行文件 .NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3285000/