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 屬性不需要表示可執行檔。 它可以是擴充功能已與系統上所安裝之應用程式相關聯的任何檔案類型。 例如, FileName 如果您有與編輯器相關聯的文字檔,例如[記事本],則可以有.txt副檔名,或者,如果您已將.doc檔案與文字處理工具建立關聯,例如Microsoft Word,則可以有.doc。 同樣地,在 [ 執行 ] 對話方塊可以接受副檔名為或不含.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 函式,即使 CreateNoWindow 屬性值為 trueWindowStyle 屬性值為 Hidden ,也會在新視窗中啟動進程。

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

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

適用於

另請參閱