Process.Start 方法

定義

啟動處理序資源,並將其與 Process 元件相關聯。

多載

Start(String, String, String, SecureString, String)

藉由指定應用程式的名稱、一組命令列引數、使用者名稱、密碼和網域來啟動處理序資源,並將該資源與新的 Process 元件相關聯。

Start(String, String, SecureString, String)

藉由指定應用程式的名稱、使用者名稱、密碼和網域來啟動處理序資源,並將該資源與新的 Process 元件相關聯。

Start(String, String)

藉由指定應用程式的名稱和一組命令列引數來啟動處理序資源,並將該資源與新的 Process 元件相關聯。

Start(String)

藉由指定文件或應用程式檔案的名稱啟動處理序資源,並將該資源與新的 Process 元件相關聯。

Start(ProcessStartInfo)

啟動含有處理序啟動資訊 (例如,要啟動之處理序的檔名) 的參數所指定的處理序資源,並將該資源與新的 Process 元件相關聯。

Start()

啟動 (或重複使用) 這個 Process 元件的 StartInfo 屬性指定的處理序資源,並將其與元件相關聯。

Start(String, IEnumerable<String>)

藉由指定應用程式的名稱和一組命令列引數來啟動處理序資源。

Start(String, String, String, SecureString, String)

重要

此 API 不符合 CLS 規範。

藉由指定應用程式的名稱、一組命令列引數、使用者名稱、密碼和網域來啟動處理序資源,並將該資源與新的 Process 元件相關聯。

public:
 static System::Diagnostics::Process ^ Start(System::String ^ fileName, System::String ^ arguments, System::String ^ userName, System::Security::SecureString ^ password, System::String ^ domain);
[System.CLSCompliant(false)]
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static System.Diagnostics.Process? Start (string fileName, string arguments, string userName, System.Security.SecureString password, string domain);
[System.CLSCompliant(false)]
public static System.Diagnostics.Process Start (string fileName, string arguments, string userName, System.Security.SecureString password, string domain);
public static System.Diagnostics.Process Start (string fileName, string arguments, string userName, System.Security.SecureString password, string domain);
[<System.CLSCompliant(false)>]
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member Start : string * string * string * System.Security.SecureString * string -> System.Diagnostics.Process
[<System.CLSCompliant(false)>]
static member Start : string * string * string * System.Security.SecureString * string -> System.Diagnostics.Process
static member Start : string * string * string * System.Security.SecureString * string -> System.Diagnostics.Process
Public Shared Function Start (fileName As String, arguments As String, userName As String, password As SecureString, domain As String) As Process

參數

fileName
String

要在處理序中執行之應用程式檔案的名稱。

arguments
String

啟動處理程序時要傳遞的命令列引數。

userName
String

要在啟動處理序時使用的使用者名稱。

password
SecureString

SecureString,包含啟動處理序時要使用的密碼。

domain
String

要在啟動處理序時使用的網域。

傳回

與處理程序資源關聯的新 Process,或者是 null (若未啟動任何處理程序資源)。 請注意,隨著已在執行中之相同處理程序啟動的新處理程序將獨立於其他處理程序之外。 此外,啟動可能會傳回非空值處理程序,且其 HasExited 屬性會設定為 true。 在此案例中,已啟動的處理程序可能已啟用本身的現有執行個體並結束。

屬性

例外狀況

未指定檔案名稱。

開啟關聯的檔案時發生錯誤。

-或-

找不到在 fileName 中指定的檔案。

-或-

引數長度和關聯檔案完整路徑長度的總和超過 2080。 與此例外狀況相關聯的錯誤訊息可以是下列其中一項:「傳遞至系統呼叫的資料區域太小」或「拒絕存取」。

處理序物件已經處置。

Linux 或 macOS 不支援此成員 (僅限 .NET Core)。

備註

使用此多載可藉由指定其檔案名、命令列引數、使用者名稱、密碼和網域,來建立新的進程及其主要執行緒。 新進程接著會在指定認證的安全性內容中執行指定的可執行檔, (使用者、網域和密碼) 。

注意

當可執行檔位於遠端磁片磁碟機上時,您必須使用統一資源識別項 (URI) 來識別網路共用,而不是連結的磁碟機號。

注意

如果要啟動的可執行檔位址是 URL,進程就不會啟動並 null 傳回。

此多載可讓您啟動進程,而不需要先建立新的 Process 實例。 多載是建立新 Process 實例、設定 FileName 屬性的 、 UserNameArguments 、、 PasswordDomain 屬性,以及呼叫 StartProcess 實例之明確步驟的 StartInfo 替代方式。

同樣地,與 [ 執行 ] 對話方塊可以接受具有或不含.exe副檔名的可執行檔名稱相同,參數中的 fileName .exe副檔名是選擇性的。 例如,您可以將 參數設定 fileName 為 「Notepad.exe」 或 「Notepad」。 fileName如果參數代表可執行檔, arguments 參數可能會代表要處理之檔案,例如 中的 Notepad.exe myfile.txt 文字檔。

注意

檔案名必須代表多載中具有 userNamepassworddomain 參數的 Start 可執行檔。

每當您用來 Start 啟動程式時,您可能需要關閉它,或有遺失系統資源的風險。 使用 CloseMainWindowKill 關閉進程。 您可以使用其 屬性來檢查進程是否已關閉 HasExited

適用於

Start(String, String, SecureString, String)

重要

此 API 不符合 CLS 規範。

藉由指定應用程式的名稱、使用者名稱、密碼和網域來啟動處理序資源,並將該資源與新的 Process 元件相關聯。

public:
 static System::Diagnostics::Process ^ Start(System::String ^ fileName, System::String ^ userName, System::Security::SecureString ^ password, System::String ^ domain);
[System.CLSCompliant(false)]
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static System.Diagnostics.Process? Start (string fileName, string userName, System.Security.SecureString password, string domain);
[System.CLSCompliant(false)]
public static System.Diagnostics.Process Start (string fileName, string userName, System.Security.SecureString password, string domain);
public static System.Diagnostics.Process Start (string fileName, string userName, System.Security.SecureString password, string domain);
[<System.CLSCompliant(false)>]
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member Start : string * string * System.Security.SecureString * string -> System.Diagnostics.Process
[<System.CLSCompliant(false)>]
static member Start : string * string * System.Security.SecureString * string -> System.Diagnostics.Process
static member Start : string * string * System.Security.SecureString * string -> System.Diagnostics.Process
Public Shared Function Start (fileName As String, userName As String, password As SecureString, domain As String) As Process

參數

fileName
String

要在處理序中執行之應用程式檔案的名稱。

userName
String

要在啟動處理序時使用的使用者名稱。

password
SecureString

SecureString,包含啟動處理序時要使用的密碼。

domain
String

要在啟動處理序時使用的網域。

傳回

與處理程序資源關聯的新 Process,或者是 null (若未啟動任何處理程序資源)。 請注意,隨著已在執行中之相同處理程序啟動的新處理程序將獨立於其他處理程序之外。 此外,啟動可能會傳回非空值處理程序,且其 HasExited 屬性會設定為 true。 在此案例中,已啟動的處理程序可能已啟用本身的現有執行個體並結束。

屬性

例外狀況

未指定檔案名稱。

開啟關聯檔案時發生錯誤。

-或-

找不到在 fileName 中指定的檔案。

處理序物件已經處置。

Linux 或 macOS 不支援此成員 (僅限 .NET Core)。

範例

下列程式碼範例示範如何使用這個多載來啟動可執行檔,同時也示範嘗試啟動與無法執行檔案相關聯的應用程式時擲 Win32Exception 回 。

// NOTE: This example requires a text.txt file file in your Documents folder
using System;
using System.Diagnostics;
using System.Security;
using System.ComponentModel;

class Example
{
    static void Main()
    {
        Console.Write("Enter your domain: ");
        string domain = Console.ReadLine();
        Console.Write("Enter you user name: ");
        string uname = Console.ReadLine();
        Console.Write("Enter your password: ");
        SecureString password = new SecureString();
        ConsoleKeyInfo key;
        do
        {
            key = Console.ReadKey(true);

            // Ignore any key out of range.
            if (((int)key.Key) >= 33 && ((int)key.Key <= 90) && key.Key != ConsoleKey.Enter)
            {
                // Append the character to the password.
                password.AppendChar(key.KeyChar);
                Console.Write("*");
            }
            // Exit if Enter key is pressed.
        } while (key.Key != ConsoleKey.Enter);
        Console.WriteLine();

        try
        {
            Console.WriteLine("\nTrying to launch NotePad using your login information...");
            Process.Start("notepad.exe", uname, password, domain);
        }
        catch (Win32Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\";

        try
        {
            // The following call to Start succeeds if test.txt exists.
            Console.WriteLine("\nTrying to launch 'text.txt'...");
            Process.Start(path + "text.txt");
        }
        catch (Win32Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        try
        {
            // Attempting to start in a shell using this Start overload fails. This causes
            // the following exception, which is picked up in the catch block below:
            // The specified executable is not a valid application for this OS platform.
            Console.WriteLine("\nTrying to launch 'text.txt' with your login information...");
            Process.Start(path + "text.txt", uname, password, domain);
        }
        catch (Win32Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            password.Dispose();
        }
    }
}
' This sample requires a text.txt file file in your documents folder.
' You'll also need to set the startup object in the project to Sub Main.
Imports System.Diagnostics
Imports System.Security
Imports System.ComponentModel

Module Program
    Sub Main()
        Console.Write("Enter your domain: ")
        Dim domain As String = Console.ReadLine()
        Console.Write("Enter you user name: ")
        Dim uname As String = Console.ReadLine()
        Console.Write("Enter your password: ")
        Dim password As New SecureString()
        Dim key As ConsoleKeyInfo
        Do
            key = Console.ReadKey(True)

            ' Ignore any key out of range.
            If key.Key >= 33 AndAlso key.Key <= 90 AndAlso key.Key <> ConsoleKey.Enter Then
                ' Append the character to the password.
                password.AppendChar(key.KeyChar)
                Console.Write("*")
            End If
            ' Exit if Enter key is pressed.
        Loop While key.Key <> ConsoleKey.Enter
        Console.WriteLine()

        Try
            Console.WriteLine(vbCrLf + "Trying to launch NotePad using your login information...")
            Process.Start("notepad.exe", uname, password, domain)
        Catch ex As Win32Exception
            Console.WriteLine(ex.Message)
        End Try

        Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\"

        Try
            ' The following call to Start succeeds if test.txt exists.
            Console.WriteLine(vbCrLf + "Trying to launch 'text.txt'...")
            Process.Start(path + "Text.txt")
        Catch ex As Win32Exception
            Console.WriteLine(ex.Message)
        End Try

        Try
            ' Attempting to start in a shell using this Start overload fails. This causes
            ' the following exception, which is picked up in the catch block below:
            ' The specified executable is not a valid application for this OS platform.
            Console.WriteLine(vbCrLf + "Trying to launch 'text.txt' with your login information...")
            Process.Start(path + "Text.txt", uname, password, domain)
        Catch ex As Win32Exception
            Console.WriteLine(ex.Message)
        Finally
            password.Dispose()
        End Try
    End Sub
End Module

備註

使用此多載來建立新的進程及其主要執行緒,方法是指定其檔案名、使用者名稱、密碼和網域。 新進程接著會在指定認證的安全性內容中執行指定的可執行檔, (使用者、網域和密碼) 。

注意

當可執行檔位於遠端磁片磁碟機上時,您必須使用統一資源識別項 (URI) 來識別網路共用,而不是連結的磁碟機號。

注意

如果要啟動的可執行檔位址是 URL,進程就不會啟動並 null 傳回。

此多載可讓您啟動進程,而不需要先建立新的 Process 實例。 多載是建立新 Process 實例、設定 FileName 屬性的 、 UserNamePasswordDomain 屬性,以及呼叫 StartProcess 實例之明確步驟的 StartInfo 替代方案。

同樣地,與 [ 執行 ] 對話方塊可以接受具有或不含.exe副檔名的可執行檔名稱相同,參數中的 fileName .exe副檔名是選擇性的。 例如,您可以將 參數設定 fileName 為 「Notepad.exe」 或 「Notepad」。 fileName如果參數代表可執行檔, arguments 參數可能會代表要處理之檔案,例如 中的 Notepad.exe myfile.txt 文字檔。

注意

檔案名必須代表多載中具有 userNamepassworddomain 參數的 Start 可執行檔。

每當您用來 Start 啟動程式時,您可能需要關閉它,或有遺失系統資源的風險。 使用 CloseMainWindowKill 關閉進程。 您可以使用其 屬性來檢查進程是否已關閉 HasExited

適用於

Start(String, String)

藉由指定應用程式的名稱和一組命令列引數來啟動處理序資源,並將該資源與新的 Process 元件相關聯。

public:
 static System::Diagnostics::Process ^ Start(System::String ^ fileName, System::String ^ arguments);
public static System.Diagnostics.Process Start (string fileName, string arguments);
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static System.Diagnostics.Process Start (string fileName, string arguments);
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
[System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")]
public static System.Diagnostics.Process Start (string fileName, string arguments);
static member Start : string * string -> System.Diagnostics.Process
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
static member Start : string * string -> System.Diagnostics.Process
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
[<System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")>]
static member Start : string * string -> System.Diagnostics.Process
Public Shared Function Start (fileName As String, arguments As String) As Process

參數

fileName
String

要在處理序中執行之應用程式檔案的名稱。

arguments
String

啟動處理程序時要傳遞的命令列引數。

傳回

與處理程序資源關聯的新 Process,或者是 null (若未啟動任何處理程序資源)。 請注意,隨著已在執行中之相同處理程序啟動的新處理程序將獨立於其他處理程序之外。 此外,啟動可能會傳回非空值處理程序,且其 HasExited 屬性會設定為 true。 在此案例中,已啟動的處理程序可能已啟用本身的現有執行個體並結束。

屬性

例外狀況

fileNamearguments 參數為 null

開啟關聯的檔案時發生錯誤。

-或-

找不到在 fileName 中指定的檔案。

-或-

引數長度和處理序完整路徑長度的總和超過 2080。 與此例外狀況相關聯的錯誤訊息可以是下列其中一項:「傳遞至系統呼叫的資料區域太小」或「拒絕存取」。

處理序物件已經處置。

PATH 環境變數具有含引號的字串。

範例

下列範例會先繁衍 Internet Explorer 的實例,並在瀏覽器中顯示 Favorites 資料夾的內容。 然後它會啟動 Internet Explorer 的其他一些實例,並顯示某些特定頁面或網站。 最後,它會在流覽至特定網站時,以最小化視窗來啟動 Internet Explorer。

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;

// Opens the Internet Explorer application.
void OpenApplication(String^ myFavoritesPath)
{
    // Start Internet Explorer. Defaults to the home page.
    Process::Start("IExplore.exe");

    // Display the contents of the favorites folder in the browser.
    Process::Start(myFavoritesPath);
}

// Opens urls and .html documents using Internet Explorer.
void OpenWithArguments()
{
    // URLs are not considered documents. They can only be opened
    // by passing them as arguments.
    Process::Start("IExplore.exe", "www.northwindtraders.com");

    // Start a Web page using a browser associated with .html and .asp files.
    Process::Start("IExplore.exe", "C:\\myPath\\myFile.htm");
    Process::Start("IExplore.exe", "C:\\myPath\\myFile.asp");
}

// Uses the ProcessStartInfo class to start new processes,
// both in a minimized mode.
void OpenWithStartInfo()
{
    ProcessStartInfo^ startInfo = gcnew ProcessStartInfo("IExplore.exe");
    startInfo->WindowStyle = ProcessWindowStyle::Minimized;
    Process::Start(startInfo);
    startInfo->Arguments = "www.northwindtraders.com";
    Process::Start(startInfo);
}

int main()
{
    // Get the path that stores favorite links.
    String^ myFavoritesPath = Environment::GetFolderPath(Environment::SpecialFolder::Favorites);
    OpenApplication(myFavoritesPath);
    OpenWithArguments();
    OpenWithStartInfo();
}
using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
    class MyProcess
    {
        // Opens the Internet Explorer application.
        void OpenApplication(string myFavoritesPath)
        {
            // Start Internet Explorer. Defaults to the home page.
            Process.Start("IExplore.exe");

            // Display the contents of the favorites folder in the browser.
            Process.Start(myFavoritesPath);
        }

        // Opens urls and .html documents using Internet Explorer.
        void OpenWithArguments()
        {
            // url's are not considered documents. They can only be opened
            // by passing them as arguments.
            Process.Start("IExplore.exe", "www.northwindtraders.com");

            // Start a Web page using a browser associated with .html and .asp files.
            Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm");
            Process.Start("IExplore.exe", "C:\\myPath\\myFile.asp");
        }

        // Uses the ProcessStartInfo class to start new processes,
        // both in a minimized mode.
        void OpenWithStartInfo()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
            startInfo.WindowStyle = ProcessWindowStyle.Minimized;

            Process.Start(startInfo);

            startInfo.Arguments = "www.northwindtraders.com";

            Process.Start(startInfo);
        }

        static void Main()
        {
            // Get the path that stores favorite links.
            string myFavoritesPath =
                Environment.GetFolderPath(Environment.SpecialFolder.Favorites);

            MyProcess myProcess = new MyProcess();

            myProcess.OpenApplication(myFavoritesPath);
            myProcess.OpenWithArguments();
            myProcess.OpenWithStartInfo();
        }
    }
}
Imports System.Diagnostics
Imports System.ComponentModel

Namespace MyProcessSample
    Class MyProcess
        ' Opens the Internet Explorer application.
        Public Sub OpenApplication(myFavoritesPath As String)
            ' Start Internet Explorer. Defaults to the home page.
            Process.Start("IExplore.exe")

            ' Display the contents of the favorites folder in the browser.
            Process.Start(myFavoritesPath)
        End Sub

        ' Opens URLs and .html documents using Internet Explorer.
        Sub OpenWithArguments()
            ' URLs are not considered documents. They can only be opened
            ' by passing them as arguments.
            Process.Start("IExplore.exe", "www.northwindtraders.com")

            ' Start a Web page using a browser associated with .html and .asp files.
            Process.Start("IExplore.exe", "C:\myPath\myFile.htm")
            Process.Start("IExplore.exe", "C:\myPath\myFile.asp")
        End Sub

        ' Uses the ProcessStartInfo class to start new processes,
        ' both in a minimized mode.
        Sub OpenWithStartInfo()
            Dim startInfo As New ProcessStartInfo("IExplore.exe")
            startInfo.WindowStyle = ProcessWindowStyle.Minimized

            Process.Start(startInfo)

            startInfo.Arguments = "www.northwindtraders.com"

            Process.Start(startInfo)
        End Sub

        Shared Sub Main()
            ' Get the path that stores favorite links.
            Dim myFavoritesPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Favorites)

            Dim myProcess As New MyProcess()

            myProcess.OpenApplication(myFavoritesPath)
            myProcess.OpenWithArguments()
            myProcess.OpenWithStartInfo()
        End Sub
    End Class
End Namespace 'MyProcessSample

備註

使用此多載可藉由指定其檔案名和命令列引數來啟動進程資源。 多載會將資源與新的 Process 物件產生關聯。

注意

如果要啟動的可執行檔位址是 URL,則不會啟動進程並 null 傳回。

此多載可讓您啟動進程,而不需要先建立新的 Process 實例。 多載是建立新 Process 實例、設定 FileName 屬性的 和 Arguments 成員 StartInfo ,以及呼叫 StartProcess 實例的明確步驟的替代方式。

藉由指定其檔案名和引數來啟動進程,類似于在 Run Windows Start 功能表的對話方塊中輸入檔案名和命令列引數。 因此,檔案名不需要代表可執行檔。 它可以是任何副檔名已與安裝在系統上的應用程式相關聯的檔案類型。 例如,如果您有與編輯器相關聯的文字檔,例如[記事本],檔案名可以有.txt副檔名,或者如果您已與文字處理工具建立關聯.doc檔案,則它可能會有.doc,例如 Microsoft Word。 同樣地,在對話方塊可以接受具有或不含.exe副檔名的可執行檔名稱時 Run ,.exe副檔名在 參數中 fileName 是選擇性的。 例如,您可以將 參數設定 fileName 為 「Notepad.exe」 或 「記事本」。 fileName如果 參數代表可執行檔,參數 arguments 可能會代表要採取行動的檔案,例如 中的 Notepad.exe myfile.txt 文字檔。 fileName如果 參數代表命令 (.cmd) 檔案, arguments 則參數必須包含 「 /c 」 或 「 /k 」 引數,以指定命令視窗在完成之後是否結束或維持。

與其他多載不同,沒有參數的 Start 多載不是 static 成員。 當您已經建立 Process 實例並指定啟動資訊時,請使用該多載, (包括檔案名) ,而且您想要啟動進程資源,並將它與現有的 Process 實例產生關聯。 當您想要建立新的 Process 元件,而不是啟動現有元件的程式時, static 請使用其中一個多載。 這個多載和沒有參數的多載都可讓您指定要傳遞之進程資源的檔案名,以及要傳遞的命令列引數。

如果您有使用引號在系統中宣告的路徑變數,在啟動該位置中找到的任何進程時,必須完整限定該路徑。 否則,系統將不會找到路徑。 例如,如果 c:\mypath 不在路徑中,而且您使用引號加以新增: path = %path%;"c:\mypath" ,則您必須在 啟動時完整限定任何進程 c:\mypath

注意

ASP.NET 網頁和伺服器控制項程式碼會在網頁伺服器上 ASP.NET 背景工作進程的內容中執行。 如果您在 ASP.NET 網頁或伺服器控制項中使用 Start 方法,新進程會在網頁伺服器上執行,並具有限制的許可權。 此程式不會在與用戶端瀏覽器相同的內容中啟動,而且無法存取使用者桌面。

每當您用來 Start 啟動程式時,您可能需要關閉它,或有遺失系統資源的風險。 使用 CloseMainWindowKill 關閉進程。 您可以使用其 HasExited 屬性來檢查進程是否已關閉。

以下是受控執行緒中 Apartment 狀態的必要注意事項。 當 UseShellExecute 位於 true 進程元件的 StartInfo 屬性上時,請確定您已在 方法上設定 屬性 [STAThread] ,在應用程式上 main() 設定執行緒模型。 否則,Managed 執行緒可以處於 unknown 狀態或放入 MTA 狀態,後者與 發生衝突 UseShellExecutetrue 某些方法要求 Apartment 狀態不是 unknown 。 如果未明確設定狀態,當應用程式遇到這類方法時,它會預設為 MTA ,一旦設定,就無法變更 Apartment 狀態。 不過,當作業系統殼層管理執行緒時, MTA 會導致擲回例外狀況。

另請參閱

適用於

Start(String)

藉由指定文件或應用程式檔案的名稱啟動處理序資源,並將該資源與新的 Process 元件相關聯。

public:
 static System::Diagnostics::Process ^ Start(System::String ^ fileName);
public static System.Diagnostics.Process Start (string fileName);
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static System.Diagnostics.Process Start (string fileName);
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
[System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")]
public static System.Diagnostics.Process Start (string fileName);
static member Start : string -> System.Diagnostics.Process
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
static member Start : string -> System.Diagnostics.Process
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
[<System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")>]
static member Start : string -> System.Diagnostics.Process
Public Shared Function Start (fileName As String) As Process

參數

fileName
String

要在處理序中執行之文件或應用程式檔案的名稱。

傳回

與處理程序資源關聯的新 Process,或者是 null (若未啟動任何處理程序資源)。 請注意,隨著已在執行中之相同處理程序啟動的新處理程序將獨立於其他處理程序之外。 此外,啟動可能會傳回非空值處理程序,且其 HasExited 屬性會設定為 true。 在此案例中,已啟動的處理程序可能已啟用本身的現有執行個體並結束。

屬性

例外狀況

開啟關聯的檔案時發生錯誤。

-或-

找不到在 fileName 中指定的檔案。

處理序物件已經處置。

PATH 環境變數具有含引號的字串。

範例

下列範例會先繁衍 Internet Explorer 的實例,並在瀏覽器中顯示 Favorites 資料夾的內容。 然後,它會啟動 Internet Explorer 的其他一些實例,並顯示一些特定的頁面或網站。 最後,它會在流覽至特定網站時,以最小化視窗啟動 Internet Explorer。

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;

// Opens the Internet Explorer application.
void OpenApplication(String^ myFavoritesPath)
{
    // Start Internet Explorer. Defaults to the home page.
    Process::Start("IExplore.exe");

    // Display the contents of the favorites folder in the browser.
    Process::Start(myFavoritesPath);
}

// Opens urls and .html documents using Internet Explorer.
void OpenWithArguments()
{
    // URLs are not considered documents. They can only be opened
    // by passing them as arguments.
    Process::Start("IExplore.exe", "www.northwindtraders.com");

    // Start a Web page using a browser associated with .html and .asp files.
    Process::Start("IExplore.exe", "C:\\myPath\\myFile.htm");
    Process::Start("IExplore.exe", "C:\\myPath\\myFile.asp");
}

// Uses the ProcessStartInfo class to start new processes,
// both in a minimized mode.
void OpenWithStartInfo()
{
    ProcessStartInfo^ startInfo = gcnew ProcessStartInfo("IExplore.exe");
    startInfo->WindowStyle = ProcessWindowStyle::Minimized;
    Process::Start(startInfo);
    startInfo->Arguments = "www.northwindtraders.com";
    Process::Start(startInfo);
}

int main()
{
    // Get the path that stores favorite links.
    String^ myFavoritesPath = Environment::GetFolderPath(Environment::SpecialFolder::Favorites);
    OpenApplication(myFavoritesPath);
    OpenWithArguments();
    OpenWithStartInfo();
}
using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
    class MyProcess
    {
        // Opens the Internet Explorer application.
        void OpenApplication(string myFavoritesPath)
        {
            // Start Internet Explorer. Defaults to the home page.
            Process.Start("IExplore.exe");

            // Display the contents of the favorites folder in the browser.
            Process.Start(myFavoritesPath);
        }

        // Opens urls and .html documents using Internet Explorer.
        void OpenWithArguments()
        {
            // url's are not considered documents. They can only be opened
            // by passing them as arguments.
            Process.Start("IExplore.exe", "www.northwindtraders.com");

            // Start a Web page using a browser associated with .html and .asp files.
            Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm");
            Process.Start("IExplore.exe", "C:\\myPath\\myFile.asp");
        }

        // Uses the ProcessStartInfo class to start new processes,
        // both in a minimized mode.
        void OpenWithStartInfo()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
            startInfo.WindowStyle = ProcessWindowStyle.Minimized;

            Process.Start(startInfo);

            startInfo.Arguments = "www.northwindtraders.com";

            Process.Start(startInfo);
        }

        static void Main()
        {
            // Get the path that stores favorite links.
            string myFavoritesPath =
                Environment.GetFolderPath(Environment.SpecialFolder.Favorites);

            MyProcess myProcess = new MyProcess();

            myProcess.OpenApplication(myFavoritesPath);
            myProcess.OpenWithArguments();
            myProcess.OpenWithStartInfo();
        }
    }
}
Imports System.Diagnostics
Imports System.ComponentModel

Namespace MyProcessSample
    Class MyProcess
        ' Opens the Internet Explorer application.
        Public Sub OpenApplication(myFavoritesPath As String)
            ' Start Internet Explorer. Defaults to the home page.
            Process.Start("IExplore.exe")

            ' Display the contents of the favorites folder in the browser.
            Process.Start(myFavoritesPath)
        End Sub

        ' Opens URLs and .html documents using Internet Explorer.
        Sub OpenWithArguments()
            ' URLs are not considered documents. They can only be opened
            ' by passing them as arguments.
            Process.Start("IExplore.exe", "www.northwindtraders.com")

            ' Start a Web page using a browser associated with .html and .asp files.
            Process.Start("IExplore.exe", "C:\myPath\myFile.htm")
            Process.Start("IExplore.exe", "C:\myPath\myFile.asp")
        End Sub

        ' Uses the ProcessStartInfo class to start new processes,
        ' both in a minimized mode.
        Sub OpenWithStartInfo()
            Dim startInfo As New ProcessStartInfo("IExplore.exe")
            startInfo.WindowStyle = ProcessWindowStyle.Minimized

            Process.Start(startInfo)

            startInfo.Arguments = "www.northwindtraders.com"

            Process.Start(startInfo)
        End Sub

        Shared Sub Main()
            ' Get the path that stores favorite links.
            Dim myFavoritesPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Favorites)

            Dim myProcess As New MyProcess()

            myProcess.OpenApplication(myFavoritesPath)
            myProcess.OpenWithArguments()
            myProcess.OpenWithStartInfo()
        End Sub
    End Class
End Namespace 'MyProcessSample

備註

使用此多載藉由指定其檔案名來啟動進程資源。 多載會將資源與新的 Process 物件產生關聯。

注意

如果要啟動的可執行檔位址是 URL,則不會啟動進程並 null 傳回。

此多載可讓您啟動進程,而不需要先建立新的 Process 實例。 多載是建立新 Process 實例、設定 FileName 屬性成員 StartInfo ,以及 Start 呼叫 Process 實例的明確步驟的替代方式。

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

藉由指定其檔案名來啟動進程,類似于在 Run Windows Start 功能表的對話方塊中輸入資訊。 因此,檔案名不需要代表可執行檔。 它可以是任何副檔名已與安裝在系統上的應用程式相關聯的檔案類型。 例如,如果您有與編輯器相關聯的文字檔,例如[記事本],檔案名可以有.txt副檔名,或者如果您已與文字處理工具建立關聯.doc檔案,則它可能會有.doc,例如 Microsoft Word。 同樣地,在對話方塊可以接受具有或不含.exe副檔名的可執行檔名稱時 Run ,.exe副檔名在 參數中 fileName 是選擇性的。 例如,您可以將 參數設定 fileName 為 「Notepad.exe」 或 「記事本」。

此多載不允許進程的命令列引數。 如果您需要為進程指定一或多個命令列引數,請使用 Process.Start(ProcessStartInfo)Process.Start(String, String) 多載。

與其他多載不同,沒有參數的 Start 多載不是 static 成員。 當您已經建立 Process 實例並指定啟動資訊時,請使用該多載, (包括檔案名) ,而且您想要啟動進程資源,並將它與現有的 Process 實例產生關聯。 當您想要建立新的 Process 元件,而不是啟動現有元件的程式時, static 請使用其中一個多載。 這個多載和沒有參數的多載都可讓您指定要啟動的進程資源檔名。

如果您有使用引號在系統中宣告的路徑變數,在啟動該位置中找到的任何進程時,必須完整限定該路徑。 否則,系統將不會找到路徑。 例如,如果 c:\mypath 不在路徑中,而且您使用引號加以新增: path = %path%;"c:\mypath" ,則您必須在 啟動時完整限定任何進程 c:\mypath

注意

ASP.NET 網頁和伺服器控制項程式碼會在網頁伺服器上 ASP.NET 背景工作進程的內容中執行。 如果您在 ASP.NET 網頁或伺服器控制項中使用 Start 方法,新進程會在網頁伺服器上執行,並具有限制的許可權。 此程式不會在與用戶端瀏覽器相同的內容中啟動,而且無法存取使用者桌面。

每當您用來 Start 啟動程式時,您可能需要關閉它,或有遺失系統資源的風險。 使用 CloseMainWindowKill 關閉進程。 您可以使用其 HasExited 屬性來檢查進程是否已關閉。

以下是受控執行緒中 Apartment 狀態的必要注意事項。 當 UseShellExecute 位於 true 進程元件的 StartInfo 屬性上時,請確定您已在 方法上設定 屬性 [STAThread] ,在應用程式上 main() 設定執行緒模型。 否則,Managed 執行緒可以處於 unknown 狀態或放入 MTA 狀態,後者與 發生衝突 UseShellExecutetrue 某些方法要求 Apartment 狀態不是 unknown 。 如果未明確設定狀態,當應用程式遇到這類方法時,它會預設為 MTA ,一旦設定,就無法變更 Apartment 狀態。 不過,當作業系統殼層管理執行緒時, MTA 會導致擲回例外狀況。

另請參閱

適用於

Start(ProcessStartInfo)

啟動含有處理序啟動資訊 (例如,要啟動之處理序的檔名) 的參數所指定的處理序資源,並將該資源與新的 Process 元件相關聯。

public:
 static System::Diagnostics::Process ^ Start(System::Diagnostics::ProcessStartInfo ^ startInfo);
public static System.Diagnostics.Process? Start (System.Diagnostics.ProcessStartInfo startInfo);
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static System.Diagnostics.Process? Start (System.Diagnostics.ProcessStartInfo startInfo);
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
[System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")]
public static System.Diagnostics.Process? Start (System.Diagnostics.ProcessStartInfo startInfo);
public static System.Diagnostics.Process Start (System.Diagnostics.ProcessStartInfo startInfo);
static member Start : System.Diagnostics.ProcessStartInfo -> System.Diagnostics.Process
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
static member Start : System.Diagnostics.ProcessStartInfo -> System.Diagnostics.Process
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
[<System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")>]
static member Start : System.Diagnostics.ProcessStartInfo -> System.Diagnostics.Process
Public Shared Function Start (startInfo As ProcessStartInfo) As Process

參數

startInfo
ProcessStartInfo

ProcessStartInfo,包含用來啟動處理序的資訊,包括檔名和任何命令列引數。

傳回

與處理程序資源關聯的新 Process,或者是 null (若未啟動任何處理程序資源)。 請注意,隨著已在執行中之相同處理程序啟動的新處理程序將獨立於其他處理程序之外。 此外,啟動可能會傳回非空值處理程序,且其 HasExited 屬性會設定為 true。 在此案例中,已啟動的處理程序可能已啟用本身的現有執行個體並結束。

屬性

例外狀況

startInfo 參數的 FileName 屬性沒有指定任何檔案名稱。

-或-

startInfo 參數的 UseShellExecute 屬性是 true,而 RedirectStandardInputRedirectStandardOutputRedirectStandardError 屬性也是 true

-或-

startInfo 參數的 UseShellExecute 屬性是 true,而 UserName 屬性不是 null 或空白,或者 Password 屬性不是 null

startInfo 參數為 null

處理序物件已經處置。

開啟關聯的檔案時發生錯誤。

-或-

找不到 startInfo 參數的 FileName 屬性中所指定的檔案。

-或-

引數長度和處理序完整路徑長度的總和超過 2080。 與此例外狀況相關聯的錯誤訊息可以是下列其中一項:「傳遞至系統呼叫的資料區域太小」或「拒絕存取」。

此方法在沒有殼層支援的作業系統上不受支援,例如 Nano 伺服器 (僅限 .NET Core)。

範例

下列範例會先繁衍 Internet Explorer 的實例,並在瀏覽器中顯示 Favorites 資料夾的內容。 然後,它會啟動 Internet Explorer 的其他一些實例,並顯示一些特定的頁面或網站。 最後,它會在流覽至特定網站時,以最小化視窗啟動 Internet Explorer。

如需此方法的其他用法範例,請參閱 類別的 ProcessStartInfo 個別屬性。

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;

// Opens the Internet Explorer application.
void OpenApplication(String^ myFavoritesPath)
{
    // Start Internet Explorer. Defaults to the home page.
    Process::Start("IExplore.exe");

    // Display the contents of the favorites folder in the browser.
    Process::Start(myFavoritesPath);
}

// Opens urls and .html documents using Internet Explorer.
void OpenWithArguments()
{
    // URLs are not considered documents. They can only be opened
    // by passing them as arguments.
    Process::Start("IExplore.exe", "www.northwindtraders.com");

    // Start a Web page using a browser associated with .html and .asp files.
    Process::Start("IExplore.exe", "C:\\myPath\\myFile.htm");
    Process::Start("IExplore.exe", "C:\\myPath\\myFile.asp");
}

// Uses the ProcessStartInfo class to start new processes,
// both in a minimized mode.
void OpenWithStartInfo()
{
    ProcessStartInfo^ startInfo = gcnew ProcessStartInfo("IExplore.exe");
    startInfo->WindowStyle = ProcessWindowStyle::Minimized;
    Process::Start(startInfo);
    startInfo->Arguments = "www.northwindtraders.com";
    Process::Start(startInfo);
}

int main()
{
    // Get the path that stores favorite links.
    String^ myFavoritesPath = Environment::GetFolderPath(Environment::SpecialFolder::Favorites);
    OpenApplication(myFavoritesPath);
    OpenWithArguments();
    OpenWithStartInfo();
}
using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
    class MyProcess
    {
        // Opens the Internet Explorer application.
        void OpenApplication(string myFavoritesPath)
        {
            // Start Internet Explorer. Defaults to the home page.
            Process.Start("IExplore.exe");

            // Display the contents of the favorites folder in the browser.
            Process.Start(myFavoritesPath);
        }

        // Opens urls and .html documents using Internet Explorer.
        void OpenWithArguments()
        {
            // url's are not considered documents. They can only be opened
            // by passing them as arguments.
            Process.Start("IExplore.exe", "www.northwindtraders.com");

            // Start a Web page using a browser associated with .html and .asp files.
            Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm");
            Process.Start("IExplore.exe", "C:\\myPath\\myFile.asp");
        }

        // Uses the ProcessStartInfo class to start new processes,
        // both in a minimized mode.
        void OpenWithStartInfo()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
            startInfo.WindowStyle = ProcessWindowStyle.Minimized;

            Process.Start(startInfo);

            startInfo.Arguments = "www.northwindtraders.com";

            Process.Start(startInfo);
        }

        static void Main()
        {
            // Get the path that stores favorite links.
            string myFavoritesPath =
                Environment.GetFolderPath(Environment.SpecialFolder.Favorites);

            MyProcess myProcess = new MyProcess();

            myProcess.OpenApplication(myFavoritesPath);
            myProcess.OpenWithArguments();
            myProcess.OpenWithStartInfo();
        }
    }
}
Imports System.Diagnostics
Imports System.ComponentModel

Namespace MyProcessSample
    Class MyProcess
        ' Opens the Internet Explorer application.
        Public Sub OpenApplication(myFavoritesPath As String)
            ' Start Internet Explorer. Defaults to the home page.
            Process.Start("IExplore.exe")

            ' Display the contents of the favorites folder in the browser.
            Process.Start(myFavoritesPath)
        End Sub

        ' Opens URLs and .html documents using Internet Explorer.
        Sub OpenWithArguments()
            ' URLs are not considered documents. They can only be opened
            ' by passing them as arguments.
            Process.Start("IExplore.exe", "www.northwindtraders.com")

            ' Start a Web page using a browser associated with .html and .asp files.
            Process.Start("IExplore.exe", "C:\myPath\myFile.htm")
            Process.Start("IExplore.exe", "C:\myPath\myFile.asp")
        End Sub

        ' Uses the ProcessStartInfo class to start new processes,
        ' both in a minimized mode.
        Sub OpenWithStartInfo()
            Dim startInfo As New ProcessStartInfo("IExplore.exe")
            startInfo.WindowStyle = ProcessWindowStyle.Minimized

            Process.Start(startInfo)

            startInfo.Arguments = "www.northwindtraders.com"

            Process.Start(startInfo)
        End Sub

        Shared Sub Main()
            ' Get the path that stores favorite links.
            Dim myFavoritesPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Favorites)

            Dim myProcess As New MyProcess()

            myProcess.OpenApplication(myFavoritesPath)
            myProcess.OpenWithArguments()
            myProcess.OpenWithStartInfo()
        End Sub
    End Class
End Namespace 'MyProcessSample

備註

藉由指定 ProcessStartInfo 實例,使用此多載來啟動進程資源。 多載會將資源與新的 Process 物件產生關聯。

注意

如果要啟動的可執行檔位址是 URL,進程就不會啟動並 null 傳回。

此多載可讓您啟動進程,而不需要先建立新的 Process 實例。 搭配參數使用此多載 ProcessStartInfo 是建立新 Process 實例、設定其 StartInfo 屬性,以及呼叫 Start 實例之明確步驟的 Process 替代方案。

ProcessStartInfo使用 實例做為 參數,可讓您 Start 以最充分的控制傳遞至呼叫的內容來啟動進程。 如果您只需要傳遞檔案名或檔案名和引數,則不需要建立新的 ProcessStartInfo 實例,雖然這是選項。 唯 Process.StartInfo 一必須設定的屬性是 FileName 屬性。 屬性 FileName 不需要表示可執行檔。 它可以是擴充功能已與系統上所安裝之應用程式相關聯的任何檔案類型。 例如, FileName 如果您有與編輯器相關聯的文字檔,例如[記事本],屬性可以有.txt副檔名,或者,如果您已將.doc檔案與文字處理工具相關聯,則可以有.doc副檔名,例如Microsoft Word。

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

ProcessStartInfo.UserName如果已設定 實例的 StartInfoProcessStartInfo.Password 屬性,則會呼叫 Unmanaged CreateProcessWithLogonW 函式,即使 ProcessStartInfo.CreateNoWindow 屬性值為 trueProcessStartInfo.WindowStyle 屬性值為 ProcessWindowStyle.Hidden ,也會在新視窗中啟動進程。 ProcessStartInfo.Domain如果屬性為 null ,則 ProcessStartInfo.UserName 屬性必須是 UPN 格式,使用者@DNS_domain_name

不同于其他多載,沒有參數的 Start 多載不是 static 成員。 當您已經建立 Process 實例並指定啟動資訊時,請使用該多載, (包括檔案名) ,而且您想要啟動進程資源,並將它與現有的 Process 實例產生關聯。 當您想要建立新的 Process 元件,而不是啟動現有元件的程式時,請使用其中 static 一個多載。 這個多載和沒有參數的多載都可讓您使用 ProcessStartInfo 實例來指定進程資源的啟動資訊。

如果您在系統中使用引號宣告路徑變數,則必須在該位置啟動任何找到的進程時,完整限定該路徑。 否則,系統將不會找到路徑。 例如,如果 c:\mypath 不在路徑中,而且您使用引號新增它: path = %path%;"c:\mypath" ,則您必須在啟動它時完整限定中的任何 c:\mypath 進程。

注意

ASP.NET 網頁和伺服器控制程式代碼會在網頁伺服器上 ASP.NET 背景工作進程的內容中執行。 如果您在 ASP.NET 網頁或伺服器控制項中使用 Start 方法,新進程就會在具有限制許可權的 Web 服務器上執行。 此程式不會在與用戶端瀏覽器相同的內容中啟動,而且無法存取使用者桌面。

每當您用來 Start 啟動程式時,您可能需要關閉它,或有遺失系統資源的風險。 使用 CloseMainWindowKill 關閉進程。 您可以使用其 屬性來檢查進程是否已關閉 HasExited

這裡需要有關 Managed 執行緒中 Apartment 狀態的附注。 當 UseShellExecutetrue 參數上 startInfo 時,請確定您已在 方法上設定 屬性 [STAThread]main() ,在應用程式中設定執行緒模型。 否則,Managed 執行緒可以處於 unknown 狀態或置於 MTA 狀態,後者與 為 true 衝突 UseShellExecute 。 某些方法需要 Apartment 狀態不是 unknown 。 如果未明確設定狀態,當應用程式遇到這類方法時,它會預設為 MTA ,一旦設定,就無法變更 Apartment 狀態。 不過,當作業系統殼層管理執行緒時, MTA 會導致擲回例外狀況。

另請參閱

適用於

Start()

啟動 (或重複使用) 這個 Process 元件的 StartInfo 屬性指定的處理序資源,並將其與元件相關聯。

public:
 bool Start();
public bool Start ();
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public bool Start ();
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
[System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")]
public bool Start ();
member this.Start : unit -> bool
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
member this.Start : unit -> bool
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
[<System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")>]
member this.Start : unit -> bool
Public Function Start () As Boolean

傳回

如果啟動處理序資源,則為 true;如果沒有啟動任何新的處理序資源 (例如,如果重複使用現有的處理序),則為 false

屬性

例外狀況

Process 元件的 StartInfo 中未指定檔案名稱。

-或-

UseShellExecute 屬性的 StartInfo 成員是 true ,而 RedirectStandardInputRedirectStandardOutputRedirectStandardErrortrue

開啟關聯檔案時發生錯誤。

處理序物件已經處置。

此方法在沒有殼層支援的作業系統上不受支援,例如 Nano 伺服器 (僅限 .NET Core)。

範例

下列範例會使用 類別的 Process 實例來啟動進程。

#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

備註

使用此多載來啟動進程資源,並將它與目前的 Process 元件產生關聯。 傳回值 true 表示已啟動新的進程資源。 如果 屬性成員 StartInfoFileName 指定的進程資源已在電腦上執行,則不會啟動其他進程資源。 相反地,會重複使用執行中的進程資源並 false 傳回。

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

注意

如果您使用 Visual Studio,此方法的多 Start 載就是您在將元件拖曳 Process 至設計工具之後,插入程式碼中的多載。 Properties使用視窗展開類別, StartInfo 並將適當的值 FileName 寫入 屬性。 您的變更會出現在表單的程式 InitializeComponent 中。

的這個多載 Start 不是 static 方法。 您必須從 類別的實例呼叫 Process 它。 呼叫 Start 之前,您必須先指定 StartInfo 這個 Process 實例的屬性資訊,因為該資訊是用來判斷要啟動的進程資源。

方法的其他多載 Startstatic 成員。 呼叫 方法的多載之前,您不需要建立元件的實例 Process 。 相反地,您可以呼叫 StartProcess 類別本身,並在進程啟動時建立新的 Process 元件。 或者,如果重複使用進程, null 則會傳回 。 進程資源會自動與 方法所 Start 傳回的新 Process 元件相關聯。

成員 StartInfo 可用來複製 Windows Start 功能表對話方塊的功能 Run 。 您可以在 屬性中設定適當的值,以啟動可輸入命令列的任何 StartInfo 專案。 唯 StartInfo 一必須設定的屬性是 FileName 屬性。 屬性 FileName 不一定是可執行檔。 它可以是擴充功能已與系統上所安裝之應用程式相關聯的任何檔案類型。 例如, FileName 如果您有與編輯器相關聯的文字檔,例如[記事本],屬性可以有.txt副檔名,或者,如果您已將.doc檔案與文字處理工具相關聯,則可以有.doc副檔名,例如Microsoft Word。

在命令列中,您可以指定要針對特定類型的檔案採取的動作。 例如,您可以列印檔案或編輯文字檔。 使用 Verb 屬性的成員來 StartInfo 指定這些動作。 針對其他類型的檔案,您可以在從 Run 對話方塊啟動檔案時指定命令列引數。 例如,如果您將瀏覽器指定為 ,您可以傳遞 URL 做為 FileName 引數。 您可以在屬性 Arguments 的成員中 StartInfo 指定這些引數。

如果您在系統中使用引號宣告路徑變數,則必須在該位置啟動任何找到的進程時,完整限定該路徑。 否則,系統將不會找到路徑。 例如,如果 c:\mypath 不在路徑中,而且您使用引號新增它: path = %path%;"c:\mypath" ,則您必須在啟動它時完整限定中的任何 c:\mypath 進程。

注意

ASP.NET 網頁和伺服器控制程式代碼會在網頁伺服器上 ASP.NET 背景工作進程的內容中執行。 如果您在 ASP.NET 網頁或伺服器控制項中使用 Start 方法,新進程就會在具有限制許可權的 Web 服務器上執行。 此程式不會在與用戶端瀏覽器相同的內容中啟動,而且無法存取使用者桌面。

每當您用來 Start 啟動程式時,您可能需要關閉它,或有遺失系統資源的風險。 使用 CloseMainWindowKill 關閉進程。 您可以使用其 屬性來檢查進程是否已關閉 HasExited

以下是受控執行緒中 Apartment 狀態的必要注意事項。 當 UseShellExecute 位於 true 進程元件的 StartInfo 屬性上時,請確定您已在 方法上設定 屬性 [STAThread] ,在應用程式上 main() 設定執行緒模型。 否則,Managed 執行緒可以處於 unknown 狀態或放入 MTA 狀態,後者與 發生衝突 UseShellExecutetrue 某些方法要求 Apartment 狀態不是 unknown 。 如果未明確設定狀態,當應用程式遇到這類方法時,它會預設為 MTA ,一旦設定,就無法變更 Apartment 狀態。 不過,當作業系統殼層管理執行緒時, MTA 會導致擲回例外狀況。

另請參閱

適用於

Start(String, IEnumerable<String>)

藉由指定應用程式的名稱和一組命令列引數來啟動處理序資源。

public:
 static System::Diagnostics::Process ^ Start(System::String ^ fileName, System::Collections::Generic::IEnumerable<System::String ^> ^ arguments);
public static System.Diagnostics.Process Start (string fileName, System.Collections.Generic.IEnumerable<string> arguments);
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static System.Diagnostics.Process Start (string fileName, System.Collections.Generic.IEnumerable<string> arguments);
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
[System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")]
public static System.Diagnostics.Process Start (string fileName, System.Collections.Generic.IEnumerable<string> arguments);
static member Start : string * seq<string> -> System.Diagnostics.Process
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
static member Start : string * seq<string> -> System.Diagnostics.Process
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
[<System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")>]
static member Start : string * seq<string> -> System.Diagnostics.Process
Public Shared Function Start (fileName As String, arguments As IEnumerable(Of String)) As Process

參數

fileName
String

要在處理序中執行之文件或應用程式檔案的名稱。

arguments
IEnumerable<String>

啟動處理序時要傳遞的命令列引數。

傳回

與處理程序資源關聯的新 Process,或者是 null (若未啟動任何處理程序資源)。

屬性

備註

如有需要,每個引數都會自動逸出。

適用於