当我们需要UseShellExecute设置为True?(When do we need to set UseShellExecute to 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; }
If we spawn a new process, when do we need to set UseShellExecute to True?
解决方案
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.
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):
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
For example:
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 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).
In summary, you should set UseShellExecute to false if:
You want to redirect the standard input / output / error (this is the most common reason)
You don't want to search the path for the executable (e.g. for security reasons)
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.
// //摘要: //获取或设置一个值,指示是否使用操作系统的shell //启动进程。 // //返回结果: //真正使用启动进程时的壳;否则,该过程是 //直接从可执行文件创建。默认值是true。 [默认值(真) [MonitoringDescription(ProcessUseShellExecute)] [NotifyParentProperty(真) 公共BOOL UseShellExecute {搞定;组; }
如果我们产生一个新的过程中,当我们需要UseShellExecute设置为True?
解决方案
的 UseShellExecute 布尔属性是关系到使用的Windows的ShellExecute 功能相较于的的CreateProcess 功能 - 简单的答案是,如果 UseShellExecute 为真,那么过程类会使用的ShellExecute 功能,否则它会使用的CreateProcess 。
较长的答案是,的ShellExecute 函数用来打开指定的程序或文件 - 这大约是equivalnt到键入要执行的命令到运行对话框,并点击确定,这意味着它可以用于(例如):
使用默认的浏览器,而无需
开启.html文件或网页知道浏览器是什么,
打开一个word文档,而无需知道什么Word中的安装路径是
运行批处理文件
运行上的路径中的任何命令
例如:
进程p =新工艺(); p.StartInfo.UseShellExecute = TRUE; p.StartInfo.FileName =www.google.co.uk; p.Start();
这是非常简单易用,灵活和强大但是带有一些弊端:
这是不可能的重定向标准输入/输出/错误句柄
这是不可能指定的子进程的安全描述符(或其他很酷的事情)
-
有是引入安全漏洞,如果你对什么将实际运行的假设潜在的:
//如果有一个名为的道路上的notepad.exe的地方可执行 //那么这可能不是做什么,我们期待 p.StartInfo.FileName =的notepad.exe p.Start();
的CreateProcess 是一个启动过程的更为precise方式 - 它不会搜索的路径,并允许您重定向的标准输入或输出子进程(除其他事项外)。 的CreateProcess 的缺点却是,没有一个实例4上面我给了就可以了(尝试一下,看看)。
总之,你应该设置 UseShellExecute 为false:
您想重定向标准输入/输出/错误(这是最常见的原因)
您不想搜索路径可执行文件(例如,出于安全原因)
相反,你应该保持 UseShellExecute true,如果你想打开的文件,URL或批处理文件等等,而不必明确给出路径的可执行文件。