当我们需要UseShellExecute设置为True? [英] When do we need to set UseShellExecute to True?

查看:2656
本文介绍了当我们需要UseShellExecute设置为True?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    //
    // Summary:
    //     Gets or sets a value indicating whether to use the operating system shell
    //     to start the process.
    //
    // Returns:
    //     true to use the shell when starting the process; otherwise, the process is
    //     created directly from the executable file. The default is true.
    [DefaultValue(true)]
    [MonitoringDescription("ProcessUseShellExecute")]
    [NotifyParentProperty(true)]
    public bool UseShellExecute { get; set; }

如果我们产生一个新的过程中,当我们需要UseShellExecute设置为True?

If we spawn a new process, when do we need to set UseShellExecute to True?

推荐答案

UseShellExecute 布尔属性是关系到使用的Windows的ShellExecute 功能相较于的的CreateProcess 功能 - 简单的答案是,如果 UseShellExecute 为真,那么过程类会使用的ShellExecute 功能,否则它会使用的CreateProcess

The UseShellExecute boolean property is related to the use of the windows ShellExecute function vs the CreateProcess function - the short answer is that if UseShellExecute is true then the Process class will use the ShellExecute function, otherwise it will use CreateProcess.

较长的答案是,的ShellExecute 函数用来打开指定的程序或文件 - 这大约是equivalnt到键入要执行的命令到运行对话框,并点击确定,这意味着它可以用于(例如):

The longer answer is that the ShellExecute function is used to open a specified program or file - it is roughly equivalnt to typing the command to be executed into the run dialog and clicking OK, which means that it can be used to (for example):


    使用默认的浏览器,而无需
  • 开启.html文件或网页知道浏览器是什么,

  • 打开一个word文档,而无需知道什么Word中的安装路径是

  • 运行批处理文件

  • 运行上的路径中的任何命令

  • Open .html files or web using the default browser without needing to know what that browser is,
  • Open a word document without needing to know what the installation path for Word is
  • Run batch files
  • Run any command on the PATH

例如:

Process p = new Process();
p.StartInfo.UseShellExecute = true;
p.StartInfo.FileName = "www.google.co.uk";
p.Start();

这是非常简单易用,灵活和强大但是带有一些弊端:

It is very easy to use, versatile and powerful however comes with some drawbacks:


  • 这是不可能的重定向标准输入/输出/错误句柄

  • 这是不可能指定的子进程的安全描述符(或其他很酷的事情)

  • 有是引入安全漏洞,如果你对什么将实际运行的假设潜在的:

  • It isn't possible to redirect the standard input / output / error handles
  • It isn't possibly to specify security descriptors (or other cool things) for the child process
  • There is a potential to introduce security vulnerabilities if you make assumptions about what will actually be run:

// If there is an executable called "notepad.exe" somewhere on the path 
// then this might not do what we expect
p.StartInfo.FileName = "notepad.exe";
p.Start();


的CreateProcess 是一个启动过程的更为precise方式 - 它不会搜索的路径,并允许您重定向的标准输入或输出子进程(除其他事项外)。 的CreateProcess 的缺点却是,没有一个实例4上面我​​给了就可以了(尝试一下,看看)。

CreateProcess is a far more precise way of starting a process - it doesn't search the path and allows you to redirect the standard input or output of the child process (among other things). The disadvantage of CreateProcess however is that none of the 4 examples I gave above will work (try it and see).

总之,你应该设置 UseShellExecute 为false:

In summary, you should set UseShellExecute to false if:


  • 您想重定向标准输入/输出/错误(这是最常见的原因)

  • 您不想搜索路径可执行文件(例如,出于安全原因)

相反,你应该保持 UseShellExecute true,如果你想打开的文件,URL或批处理文件等等,而不必明确给出路径的可执行文件。

Conversely you should keep UseShellExecute true if you want to open documents, urls or batch files etc... rather than having to explicitly give the path to an executable.

这篇关于当我们需要UseShellExecute设置为True?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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