什么时候需要将ProcessStartInfo.UseShellExecute设置为True? [英] When do we need to set ProcessStartInfo.UseShellExecute to True?

查看:413
本文介绍了什么时候需要将ProcessStartInfo.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 为true,则 Process 类将使用 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 函数是用于打开指定的程序或文件-与在运行对话框中键入要执行的命令并单击确定大致等效,这意味着它可以是我们例如:

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的安装路径是什么

  • PATH 上运行任何命令

  • 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 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 是启动进程的一种更为精确的方法-它不搜索路径,并允许您重定向子进程的标准输入或输出(除其他外)。 CreateProcess 的缺点是,我上面给出的3个示例都不起作用(尝试并查看)。

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 3 examples I gave above will work (try it and see).

总结,则在以下情况下应将 UseShellExecute 设置为false:

In summary, you should set UseShellExecute to false if:


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

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

如果要打开文档,URL或批处理文件等,则应保持 UseShellExecute true,而不是必须显式提供可执行文件。

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.

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

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