ProcessStartInfo.UseShellExecute 屬性

定義

取得或設定值,表示是否要使用作業系統 Shell 來啟動處理序。Gets or sets a value indicating whether to use the operating system shell to start the process.

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,則為 true,如果處理序應直接從可執行檔建立,則為 falsetrue if the shell should be used when starting the process; false if the process should be created directly from the executable file. .NET Framework apps 上的預設值為 true,而在 .NET Core 應用程式上為 falseThe default is true on .NET Framework apps and false on .NET Core apps.

例外狀況

嘗試在通用 Windows 平台 (UWP) App 上將值設定為 trueAn attempt to set the value to true on Universal Windows Platform (UWP) apps occurs.

範例

// Run "cl.exe /cld stdstr.cpp /link /out:sample.exe". 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 cl.exe is directed to the Process.StandardOutput stream
// which is then displayed in this console window directly.    
Process^ compiler = gcnew Process;
compiler->StartInfo->FileName = "cl.exe";
compiler->StartInfo->Arguments = "/clr stdstr.cpp /link /out:sample.exe";
compiler->StartInfo->UseShellExecute = false;
compiler->StartInfo->RedirectStandardOutput = true;
compiler->Start();

Console::WriteLine( compiler->StandardOutput->ReadToEnd() );

compiler->WaitForExit();
// 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

備註

將此屬性設定為 false,可讓您重新導向輸入、輸出和錯誤資料流程。Setting this property to false enables you to redirect input, output, and error streams.

此內容中的 "shell" 這個字(UseShellExecute)指的是圖形化介面(類似于 Windows shell),而不是命令介面(例如,bashsh),可讓使用者啟動圖形化應用程式或開啟檔。The word "shell" in this context (UseShellExecute) refers to a graphical shell (similar to the Windows shell) rather than command shells (for example, bash or sh) and lets users launch graphical applications or open documents.

注意

如果 UserName 屬性不是 null 或空字串,則 UseShellExecute 必須 false; 當呼叫 Process.Start(ProcessStartInfo) 方法時,將會擲回 InvalidOperationExceptionUseShellExecute must be false if the UserName property is not null or an empty string, or an InvalidOperationException will be thrown when the Process.Start(ProcessStartInfo) method is called.

當您使用作業系統 shell 來啟動進程時,您可以啟動任何檔(這是與具有預設開啟動作之可執行檔相關聯的任何已註冊檔案類型),並使用 Process 物件執行檔案的作業(例如列印)。When you use the operating system shell to start processes, you can start any document (which is any registered file type associated with an executable that has a default open action) and perform operations on the file, such as printing, by using the Process object. UseShellExecute false 時,您只能使用 Process 物件來啟動可執行檔。When UseShellExecute is false, you can start only executables by using the Process object.

注意

如果您將 ErrorDialog 屬性設定為 trueUseShellExecute 必須 trueUseShellExecute must be true if you set the ErrorDialog property to true.

如果您將 WindowStyle 設定為 ProcessWindowStyle.HiddenUseShellExecute 必須設定為 trueIf you set the WindowStyle to ProcessWindowStyle.Hidden, UseShellExecute must be set to true.

WorkingDirectoryWorkingDirectory

WorkingDirectory 屬性的行為會根據 UseShellExecute 屬性的值而有所不同。The WorkingDirectory property behaves differently depending on the value of the UseShellExecute property. UseShellExecute true 時,WorkingDirectory 屬性會指定可執行檔的位置。When UseShellExecute is true, the WorkingDirectory property specifies the location of the executable. 如果 WorkingDirectory 是空字串,則會假設目前的目錄包含可執行檔。If WorkingDirectory is an empty string, it is assumed that the current directory contains the executable.

UseShellExecute false 時,不會使用 WorkingDirectory 屬性來尋找可執行檔。When UseShellExecute is false, the WorkingDirectory property is not used to find the executable. 相反地,它只會由已啟動的進程使用,而且只有在新進程的內容中才有意義。Instead, it is used only by the process that is started and has meaning only within the context of the new process. UseShellExecute false 時,FileName 屬性可以是可執行檔的完整路徑,或是系統會嘗試在 PATH 環境變數所指定的資料夾內尋找的簡單可執行檔名稱。When UseShellExecute is false, the FileName property can be either a fully qualified path to the executable, or a simple executable name that the system will attempt to find within folders specified by the PATH environment variable.

適用於

另請參閱