Process.StartInfo 屬性

定義

取得或設定要傳遞給 ProcessStart() 方法的屬性。

public:
 property System::Diagnostics::ProcessStartInfo ^ StartInfo { System::Diagnostics::ProcessStartInfo ^ get(); void set(System::Diagnostics::ProcessStartInfo ^ value); };
public System.Diagnostics.ProcessStartInfo StartInfo { get; set; }
[System.ComponentModel.Browsable(false)]
public System.Diagnostics.ProcessStartInfo StartInfo { get; set; }
member this.StartInfo : System.Diagnostics.ProcessStartInfo with get, set
[<System.ComponentModel.Browsable(false)>]
member this.StartInfo : System.Diagnostics.ProcessStartInfo with get, set
Public Property StartInfo As ProcessStartInfo

屬性值

ProcessStartInfo,代表用來啟動處理序的資料。 這些引數包括用來啟動處理序的可執行檔或文件的名稱。

屬性

例外狀況

指定 StartInfo 的值是 null

僅限 .NET Core 和 .NET 5+: Start() 方法未用來啟動進程。

範例

下列範例會 StartInfo 填入 要執行的檔案、對其執行的動作,以及它是否應該顯示使用者介面。 如需其他範例,請參閱 類別屬性的 ProcessStartInfo 參考頁面。

#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;

int main()
{
    Process^ myProcess = gcnew Process;

    try
    {
        myProcess->StartInfo->UseShellExecute = false;
        // You can start any process, HelloWorld is a do-nothing example.
        myProcess->StartInfo->FileName = "C:\\HelloWorld.exe";
        myProcess->StartInfo->CreateNoWindow = true;
        myProcess->Start();
        // This code assumes the process you are starting will terminate itself. 
        // Given that it is started without a window so you cannot terminate it 
        // on the desktop, it must terminate itself or you can do it programmatically
        // from this application using the Kill method.
    }
    catch ( Exception^ e ) 
    {
        Console::WriteLine( e->Message );
    }
}
using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
    class MyProcess
    {
        public static void Main()
        {
            try
            {
                using (Process myProcess = new Process())
                {
                    myProcess.StartInfo.UseShellExecute = false;
                    // You can start any process, HelloWorld is a do-nothing example.
                    myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
                    myProcess.StartInfo.CreateNoWindow = true;
                    myProcess.Start();
                    // This code assumes the process you are starting will terminate itself.
                    // Given that it is started without a window so you cannot terminate it
                    // on the desktop, it must terminate itself or you can do it programmatically
                    // from this application using the Kill method.
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}
Imports System.Diagnostics
Imports System.ComponentModel

Namespace MyProcessSample
    Class MyProcess
        Public Shared Sub Main()
            Try
                Using myProcess As New Process()

                    myProcess.StartInfo.UseShellExecute = False
                    ' You can start any process, HelloWorld is a do-nothing example.
                    myProcess.StartInfo.FileName = "C:\\HelloWorld.exe"
                    myProcess.StartInfo.CreateNoWindow = True
                    myProcess.Start()
                    ' This code assumes the process you are starting will terminate itself. 
                    ' Given that it is started without a window so you cannot terminate it 
                    ' on the desktop, it must terminate itself or you can do it programmatically
                    ' from this application using the Kill method.
                End Using
            Catch e As Exception
                Console.WriteLine((e.Message))
            End Try
        End Sub
    End Class
End Namespace

備註

StartInfo 表示用來啟動進程的參數集。 呼叫 時 StartStartInfo 會用來指定要啟動的進程。 唯一設定的必要 StartInfo 成員是 FileName 屬性。 藉由指定 FileName 屬性來啟動進程類似於在 Windows [開始] 選單的 [執行] 對話框中輸入資訊。 因此, FileName 屬性不需要表示可執行檔。 它可以是任何擴展名已與安裝在系統上的應用程式相關聯的檔類型。 例如 .txt,FileName如果您有與編輯器相關聯的文本檔,例如[記事本],或者如果您已與文字處理工具建立關聯 .doc 檔案,則此擴展名可能會有 .doc,例如 Microsoft Word。 同樣地,在 [ 執行 ] 對話框可以接受具有或不含 .exe 擴展名的可執行檔名稱時,.exe 擴展名在成員中 FileName 是選擇性的。 例如,您可以將 屬性設定 FileName 為 「Notepad.exe」 或 「Notepad」。

您可以將 屬性設定 FileName 為位置 (來啟動 ClickOnce 應用程式,例如,您原本安裝應用程式的網址) 。 請勿在硬碟上指定其安裝位置,以啟動 ClickOnce 應用程式。

如果檔名牽涉到不可執行的檔案,例如 .doc 檔案,您可以包含動詞,以指定要對檔案採取的動作。 例如,您可以針對結尾為 .doc 擴展名的檔案, Verb 將 設定為 “Print”。 如果您手動輸入 屬性的值Verb,屬性中指定的FileName檔名就不需要有擴展名。 不過,如果您使用 Verbs 屬性來判斷可用的動詞,則必須包含延伸模組。

您可以將 屬性中指定的 StartInfo 參數變更為在進程上呼叫 Start 方法的時間。 啟動程序之後,變更 StartInfo 值不會影響或重新啟動相關聯的進程。 如果您使用 和 ProcessStartInfo.Password 屬性集呼叫 Start(ProcessStartInfo) 方法ProcessStartInfo.UserName,則會呼叫 Unmanaged CreateProcessWithLogonW 函式,即使屬性值為 true 或 屬性值為 Hidden,也會WindowStyle在新視窗中CreateNoWindow啟動進程。

您應該只存取 StartInfo 方法所Start傳回之 物件上的 Process 屬性。 例如,您不應該在 所GetProcesses傳回的物件上Process存取 StartInfo 屬性。 否則,在 .NET Core 上,StartInfo屬性會擲InvalidOperationException回 ,並在 .NET Framework 傳回虛擬ProcessStartInfo物件。

啟動行程時,檔名是填入 (只讀) 屬性的 MainModule 檔案。 如果您想要在進程啟動之後擷取與進程相關聯的可執行檔,請使用 MainModule 屬性。 如果您想要設定尚未啟動相關聯進程之實例的 Process 可執行檔,請使用 StartInfo 屬性 FileName 的成員。 由於 屬性的成員 StartInfo 是傳遞至 Start 進程方法的自變數,因此在相關聯的進程啟動之後變更 FileName 屬性將不會重設 MainModule 屬性。 這些屬性僅用於初始化相關聯的進程。

適用於

另請參閱