我用它來運行一個外部的exe進程:如何忽略Process.Start()「系統找不到指定的文件」?
using (Process p = new Process())
{
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "program.exe";
p.StartInfo.Arguments = "example";
p.Start();
p.WaitForExit();
string pResult = p.StandardOutput.ReadToEnd();
p.Close();
}
它工作正常,但如果Program.exe文件被移動或丟失,在p.Start整個應用程序崩潰();.
即使program.exe丟失,解決崩潰的最佳方法是什麼? 我想讓主應用程序保持運行,無論program.exe是否啓動。
我應該使用try/catch還是有更好的方法?
'File.Exists'? –
'File.Exists' *和* try/catch' – DavidG
忘記'File.Exists'。只使用'try/catch':https://blogs.msdn.microsoft.com/ericlippert/2008/09/10/vexing-exceptions/ –