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

查看:52
本文介绍了我们什么时候需要将 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 为真,则 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 为真...而不是必须明确给出可执行文件的路径.

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天全站免登陆