gpt4 book ai didi

c# - 捕获外部过程中的错误

转载 作者:行者123 更新时间:2023-12-03 08:15:23 26 4
gpt4 key购买 nike

如何捕获或重定向Process发生的错误?

我希望能够得到错误的味精信息。

下面的代码不执行此操作。

try
{
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = process_to_run;
myProcess.StartInfo.Arguments = p_arg + " > "+process_to_run+".txt 2>&1";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
myProcess.WaitForExit();

}
catch (Exception ep)
{
richTextBox1.AppendText("\n" + ep.Message);
time_date = get_time();
log_file.Write(time_date);
log_file.WriteLine(process_to_run + "...Failed. ERROR: " + ep.Message);
}

最佳答案

您可以redirect the standard error,然后对其进行分析以确定是否有错误。

// Start the child process.
Process p = new Process();
// Redirect the error stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected error stream.
// p.WaitForExit();
// Read the error stream first and then wait.
string error = p.StandardError.ReadToEnd();
p.WaitForExit();

关于c# - 捕获外部过程中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11252494/

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