C# 即使进程已启动,Start也返回false
我们在应用程序中具有以下功能:C# 即使进程已启动,Start也返回false,c#,.net,wpf,C#,.net,Wpf,我们在应用程序中具有以下功能: public static Process StartApp(string fileName, bool waitForProcessExit) { Process proc = new Process { StartInfo = { FileName = fileName, UseShellExecute = true, WindowStyle
public static Process StartApp(string fileName, bool waitForProcessExit)
{
Process proc = new Process
{
StartInfo =
{
FileName = fileName,
UseShellExecute = true,
WindowStyle = ProcessWindowStyle.Normal,
ErrorDialog = false
}
};
bool processStarted = proc.Start();
if (!processStarted)
{
int errorCode = Marshal.GetLastWin32Error();
if (errorCode == 0)
{
throw new Exception("Unknown Error!");
}
throw new Win32Exception(errorCode);
}
if (waitForProcessExit)
{
proc.WaitForExit();
}
return proc;
}
执行此函数时,在某些情况下,变量processStarted
包含false
。例如,当打开Microsoft Word文件且Word已在运行时(Excel也会发生这种情况)。使用.jpg文件作为参数时,结果是相同的:Process.Start
返回false
当参数waitForProcessExit
为true时,我们的应用程序需要阻塞
我们如何才能做到这一点?从这一点看来,这是设计的行为。返回值:流程资源启动时为true;如果未启动新流程资源(例如,如果重用现有流程),则为false。这似乎是设计的行为。返回值:流程资源启动时为true;如果未启动新流程资源(例如,如果重用现有流程),则为false。