ProcessStartInfo.UseShellExecute 属性

定义

获取或设置一个值,该值指示是否使用操作系统 shell 启动进程。

public:
 property bool UseShellExecute { bool get(); void set(bool value); };
public bool UseShellExecute { get; set; }
member this.UseShellExecute : bool with get, set
Public Property UseShellExecute As Boolean

属性值

如果在启动进程时应使用 shell,则为 如果应直接从可执行文件创建进程,则为 。 默认值为 false (或 true on .NET Framework 应用)。

例外

尝试在通用 Windows 平台 (UWP)应用上将值设置为 true

示例

// Run "csc.exe /r:System.dll /out:sample.exe stdstr.cs". UseShellExecute is false because we're specifying
// an executable directly and in this case depending on it being in a PATH folder. By setting
// RedirectStandardOutput to true, the output of csc.exe is directed to the Process.StandardOutput stream
// which is then displayed in this console window directly.
using (Process compiler = new Process())
{
    compiler.StartInfo.FileName = "csc.exe";
    compiler.StartInfo.Arguments = "/r:System.dll /out:sample.exe stdstr.cs";
    compiler.StartInfo.UseShellExecute = false;
    compiler.StartInfo.RedirectStandardOutput = true;
    compiler.Start();

    Console.WriteLine(compiler.StandardOutput.ReadToEnd());

    compiler.WaitForExit();
}
' Run "vbc.exe /reference:Microsoft.VisualBasic.dll /out:sample.exe stdstr.vb". UseShellExecute is False 
' because we're specifying an executable directly and in this case depending on it being in a PATH folder. 
' By setting RedirectStandardOutput to True, the output of csc.exe is directed to the Process.StandardOutput 
' stream which is then displayed in this console window directly.    
Using compiler As New Process()
    compiler.StartInfo.FileName = "vbc.exe"
    compiler.StartInfo.Arguments = "/reference:Microsoft.VisualBasic.dll /out:sample.exe stdstr.vb"
    compiler.StartInfo.UseShellExecute = False
    compiler.StartInfo.RedirectStandardOutput = True
    compiler.Start()

    Console.WriteLine(compiler.StandardOutput.ReadToEnd())

    compiler.WaitForExit()
End Using

注解

ProcessStartInfo 类指定启动进程时使用的一组值。

设置UseShellExecute属性为false后,可以重定向输入、输出和错误流。

此上下文UseShellExecute中的“shell”一词是指图形 shell(类似于 Windows shell),而不是命令 shell(例如, bashsh),允许用户启动图形应用程序或打开文档。

注释

如果UseShellExecute属性不是false或空字符串,UserName必须是null,否则在调用InvalidOperationException方法时会引发Process.Start(ProcessStartInfo)

使用操作系统 shell 启动进程时,可以使用 Process 对象来启动任何文档(即与具有默认打开操作的可执行文件关联的任何注册文件类型),并对该文件执行操作,例如打印。 当UseShellExecutefalse时,只能使用Process对象启动可执行文件。

注释

如果将UseShellExecute属性设置为true,则ErrorDialog必须是true

WorkingDirectory

WorkingDirectory 属性的行为因 UseShellExecute 属性的值而异。 当UseShellExecutetrue时,该WorkingDirectory属性指定可执行文件的位置。 如果 WorkingDirectory 为空字符串,则假定当前目录包含可执行文件。

UseShellExecutefalse 时,不会使用 WorkingDirectory 属性来查找可执行文件。 相反,它仅由启动的进程使用,并且仅在新进程的上下文中具有意义。 如果UseShellExecutefalse,则FileName属性可以是可执行文件的完全限定路径,或者是系统会尝试在PATH环境变量指定的文件夹中查找的简单可执行文件名称。 搜索路径的解释取决于作系统。 有关详细信息,请在命令提示符处输入 HELP PATHman sh

适用于

另请参阅