gpt4 book ai didi

c# - 从 ASP.NET 启动外部进程

转载 作者:太空狗 更新时间:2023-10-29 21:11:33 25 4
gpt4 key购买 nike

我需要从我的网络应用程序启动一个外部进程。这意味着使用 System.Diagnostics.ProcessStartInfo 调用并执行控制台应用程序 (exe)。但我随后需要以某种方式确保在执行期间没有出现任何问题并知道应用程序何时完成其工作。

捕获所有可能的错误并查明错误何时完成的最佳方法是什么?

最佳答案

Process 类并不难。不过,之前的海报是正确的 - 您需要关注权限。

private string RunProcess(string cmd)
{
System.Diagnostics.Process p;
p= new System.Diagnostics.Process();
if (cmd== null || cmd=="") {
return "no command given.";
}
string[] args= cmd.Split(new char[]{' '});
if (args== null || args.Length==0 || args[0].Length==0) {
return "no command provided.";
}
p.StartInfo.FileName= args[0];

if (args.Length>1) {
int startPoint= cmd.IndexOf(' ');
string s= cmd.Substring(startPoint, cmd.Length-startPoint);
p.StartInfo.Arguments= s;
}
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;

p.Start();

// must have the readToEnd BEFORE the WaitForExit(), to avoid a deadlock condition
string output= p.StandardOutput.ReadToEnd();
p.WaitForExit();

return output;
}

关于c# - 从 ASP.NET 启动外部进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/578890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com