【问题标题】:ShellExecute vs. Process.StartShellExecute 与 Process.Start
【发布时间】:2015-06-30 16:58:42
【问题描述】:

this answer 中,作者@Abel 建议在Process.Start 不起作用时使用ShellExecute

我什么时候会使用ShellExecute - 我从来没有遇到过Process.Start 不起作用的情况?

此外,使用ShellExecuteProcess.Start 相比有什么优势吗?

【问题讨论】:

  • ShellExecute 是一个 Windows API 函数。在其他环境中不会有。
  • 因为他不了解它,因此盲目地认为它具有神奇的力量。

标签: c# wpf process


【解决方案1】:

使用 ShellExecute 优于 Process.Start 有什么优势

首先,您需要了解ShellExecute 的作用。来自ProcessStartInfo.UseShellExecute

当你使用操作系统 shell 启动进程时,你可以 启动任何文档(这是任何与 具有默认打开操作的可执行文件)并执行操作 使用 Process 对象对文件进行处理,例如打印。什么时候 UseShellExecute 为 false,您只能使用 进程对象。

这意味着它将允许您打开任何具有关联文件类型的文件,例如给定的 word 文档。否则,您只能调用可执行文件。如果您在ProcessStartInfo 中将此标志设置为true,则在内部,Process.Start will invoke the same WinAPI call

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,您只需调用托管包装器,这通常使用起来更方便。

【讨论】:

    猜你喜欢
    • 2010-09-07
    • 1970-01-01
    • 2010-12-03
    • 2012-06-25
    • 2017-01-11
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多