ShellExecute 与 Process.Start [英] ShellExecute vs. Process.Start

查看:76
本文介绍了ShellExecute 与 Process.Start的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个答案中,作者@Abel 建议每当 Process.启动将不起作用.

In this answer the author @Abel suggests using ShellExecute whenever Process.Start will not function.

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

When would I use ShellExecute - I did never have the situation that Process.Start did not work?

此外,使用 ShellExecute 比使用 Process.Start 有什么优势吗?

Furthermore, are there any advantages for using ShellExecute over Process.Start?

推荐答案

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

are there any advantages for using ShellExecute over Process.Start

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

First, you need to understand what ShellExecute does. From ProcessStartInfo.UseShellExecute:

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

When you use the operating system shell to start processes, you can start any document (which is any registered file type associated with an executable that has a default open action) and perform operations on the file, such as printing, by using the Process object. When UseShellExecute is false, you can start only executables by using the Process object.

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

This means that it will allow you to open any file that has an assosicated file type, such as a given word document. Otherwise, you can only invoke executables. If you set this flag to true in ProcessStartInfo, internally, 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,您只需调用托管包装器,这通常更方便使用.

When you invoke ShellExecute, you're using PInvoke to directly call WinAPI. With Process.Start, you're simply invoking the managed wrapper, which is usually more convenient to use.

这篇关于ShellExecute 与 Process.Start的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆