//
// 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; }
Run Code Online (Sandbox Code Playgroud)
如果我们生成一个新进程,我们什么时候需要将UseShellExecute设置为True?
Jus*_*tin 188
该UseShellExecute
布尔属性是关系到使用的Windows ShellExecute的功能VS的CreateProcess的功能-简单的答案是,如果UseShellExecute
为真,那么Process
类将使用该ShellExecute
功能,否则它会使用CreateProcess
.
更长的答案是该ShellExecute
函数用于打开指定的程序或文件 - 它大致相当于在执行对话框中键入要执行的命令并单击OK,这意味着它可以用于(例如):
PATH
例如:
Process p = new Process();
p.StartInfo.UseShellExecute = true;
p.StartInfo.FileName = "www.google.co.uk";
p.Start();
Run Code Online (Sandbox Code Playgroud)
它非常易于使用,功能多样且功能强大,但有一些缺点:
如果您对实际运行的内容做出假设,则可能会引入安全漏洞:
// 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();
Run Code Online (Sandbox Code Playgroud)CreateProcess
是一种更加精确的启动流程的方法 - 它不会搜索路径并允许您重定向子流程的标准输入或输出(以及其他内容).CreateProcess
然而,缺点是上面给出的4个例子都不起作用(试试看).
总之,UseShellExecute
如果符合以下条件,则应设置为false:
相反,UseShellExecute
如果要打开文档,URL或批处理文件等,则应该保持正确...而不必显式提供可执行文件的路径.
Che*_*hen 11
来自MSDN:
将此属性设置为false可以重定向输入,输出和错误流.
如果UserName属性不为null或为空字符串,则UseShellExecute必须为false,否则在调用Process.Start(ProcessStartInfo)方法时将抛出InvalidOperationException.
使用操作系统shell启动进程时,可以启动任何文档(与具有默认打开操作的可执行文件关联的任何已注册文件类型),并使用Process组件对文件执行操作(如打印).当UseShellExecute为false时,您只能使用Process组件启动可执行文件.
如果将ErrorDialog属性设置为true,则UseShellExecute必须为true.
归档时间: |
|
查看次数: |
78751 次 |
最近记录: |