首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ShellExecute对Process.Start

ShellExecute对Process.Start
EN

Stack Overflow用户
提问于 2015-06-30 16:58:42
回答 1查看 5.7K关注 0票数 2

这个答案中,作者@Abel建议在Process.Start不起作用时使用ShellExecute

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

此外,在ShellExecute上使用Process.Start有什么好处吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-30 17:05:26

使用ShellExecute比Process.Start有什么好处吗?

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

使用操作系统外壳启动进程时,可以启动任何文档(这是与具有默认打开操作的可执行文件关联的任何已注册文件类型),并通过使用Process对象对文件执行操作(例如打印)。如果UseShellExecute为false,则只能使用Process对象启动可执行文件。

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

代码语言:javascript
复制
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,您只需调用托管包装器,这通常更方便使用。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31144116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档