在这个答案中,作者@Abel建议在Process.Start不起作用时使用ShellExecute。
我什么时候会使用ShellExecute --我从来没有遇到过Process.Start不起作用的情况?
此外,在ShellExecute上使用Process.Start有什么好处吗?
发布于 2015-06-30 17:05:26
使用ShellExecute比Process.Start有什么好处吗?
首先,您需要了解ShellExecute的功能。来自ProcessStartInfo.UseShellExecute
使用操作系统外壳启动进程时,可以启动任何文档(这是与具有默认打开操作的可执行文件关联的任何已注册文件类型),并通过使用Process对象对文件执行操作(例如打印)。如果UseShellExecute为false,则只能使用Process对象启动可执行文件。
这意味着它将允许您打开任何具有组合文件类型的文件,例如给定的word文档。否则,只能调用可执行文件。如果在ProcessStartInfo中将此标志设置为true,则内部为Process.Start 将调用相同的WinAPI调用
public bool Start()
{
Close();
ProcessStartInfo startInfo = StartInfo;
if (startInfo.FileName.Length == 0)
throw new InvalidOperationException(SR.GetString(SR.FileNameMissing));
if (startInfo.UseShellExecute)
{
return StartWithShellExecuteEx(startInfo);
}
else
{
return StartWithCreateProcess(startInfo);
}
}当您调用ShellExecute时,您使用PInvoke直接调用WinAPI。使用Process.Start,您只需调用托管包装器,这通常更方便使用。
https://stackoverflow.com/questions/31144116
复制相似问题