Process.Start 方法

定義

啟動處理序資源,並將其與 Process 元件相關聯。Starts a process resource and associates it with a Process component.

多載

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

藉由指定應用程式的名稱、一組命令列引數、使用者名稱、密碼和網域來啟動處理序資源,並將該資源與新的 Process 元件相關聯。Starts a process resource by specifying the name of an application, a set of command-line arguments, a user name, a password, and a domain and associates the resource with a new Process component.

Start(String, String, SecureString, String)

藉由指定應用程式的名稱、使用者名稱、密碼和網域來啟動處理序資源,並將該資源與新的 Process 元件相關聯。Starts a process resource by specifying the name of an application, a user name, a password, and a domain and associates the resource with a new Process component.

Start(String, String)

藉由指定應用程式的名稱和一組命令列引數來啟動處理序資源,並將該資源與新的 Process 元件相關聯。Starts a process resource by specifying the name of an application and a set of command-line arguments, and associates the resource with a new Process component.

Start()

啟動 (或重複使用) 這個 Process 元件的 StartInfo 屬性指定的處理序資源,並將其與元件相關聯。Starts (or reuses) the process resource that is specified by the StartInfo property of this Process component and associates it with the component.

Start(String)

藉由指定文件或應用程式檔案的名稱啟動處理序資源,並將該資源與新的 Process 元件相關聯。Starts a process resource by specifying the name of a document or application file and associates the resource with a new Process component.

Start(ProcessStartInfo)

啟動含有處理序啟動資訊 (例如,要啟動之處理序的檔名) 的參數所指定的處理序資源,並將該資源與新的 Process 元件相關聯。Starts the process resource that is specified by the parameter containing process start information (for example, the file name of the process to start) and associates the resource with a new Process component.

Start(String, IEnumerable<String>)

藉由指定應用程式的名稱和一組命令列引數來啟動處理序資源。Starts a process resource by specifying the name of an application and a set of command line arguments.

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

重要

此 API 不符合 CLS 規範。

藉由指定應用程式的名稱、一組命令列引數、使用者名稱、密碼和網域來啟動處理序資源,並將該資源與新的 Process 元件相關聯。Starts a process resource by specifying the name of an application, a set of command-line arguments, a user name, a password, and a domain and associates the resource with a new Process component.

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
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
Public Shared Function Start (fileName As String, arguments As String, userName As String, password As SecureString, domain As String) As Process

參數

fileName
String

要在處理序中執行之應用程式檔案的名稱。The name of an application file to run in the process.

arguments
String

啟動處理程序時要傳遞的命令列引數。Command-line arguments to pass when starting the process.

userName
String

要在啟動處理序時使用的使用者名稱。The user name to use when starting the process.

password
SecureString

SecureString,包含啟動處理序時要使用的密碼。A SecureString that contains the password to use when starting the process.

domain
String

要在啟動處理序時使用的網域。The domain to use when starting the process.

傳回

Process

與處理程序資源關聯的新 Process,或者是 null (若未啟動任何處理程序資源)。A new Process that is associated with the process resource, or null if no process resource is started. 請注意,隨著已在執行中之相同處理程序啟動的新處理程序將獨立於其他處理程序之外。Note that a new process that's started alongside already running instances of the same process will be independent from the others. 此外,啟動可能會傳回非空值處理程序,且其 HasExited 屬性會設定為 trueIn addition, Start may return a non-null Process with its HasExited property already set to true. 在此案例中,已啟動的處理程序可能已啟用本身的現有執行個體並結束。In this case, the started process may have activated an existing instance of itself and then exited.

屬性

例外狀況

未指定檔案名稱。No file name was specified.

開啟關聯的檔案時發生錯誤。An error occurred when opening the associated file.

-或--or-

找不到在 fileName 中指定的檔案。The file specified in the fileName could not be found.

-或--or- 引數長度和關聯檔案完整路徑長度的總和超過 2080。The sum of the length of the arguments and the length of the full path to the associated file exceeds 2080. 與這個例外狀況關聯的錯誤訊息可以是下列其中之一:「傳遞到系統呼叫的資料區太小」,The error message associated with this exception can be one of the following: "The data area passed to a system call is too small." 或「拒絕存取」。or "Access is denied."

處理序物件已經處置。The process object has already been disposed.

Linux 或 macOS 不支援此成員 (僅限 .NET Core)。This member is not supported on Linux or macOS (.NET Core only).

備註

您可以使用此多載,藉由指定其檔案名、命令列引數、使用者名稱、密碼和網域來建立新的進程及其主要執行緒。Use this overload to create a new process and its primary thread by specifying its file name, command-line arguments, user name, password, and domain. 然後,新的進程會在指定認證的安全性內容中執行指定的可執行檔 (使用者、網域和密碼) 。The new process then runs the specified executable file in the security context of the specified credentials (user, domain, and password).

注意

當可執行檔位於遠端磁片磁碟機時,您必須使用統一資源識別項 (URI) 來識別網路共用,而不是連結的磁碟機號。When the executable file is located on a remote drive, you must identify the network share by using a uniform resource identifier (URI), not a linked drive letter.

注意

如果要啟動之可執行檔的位址是 URL,則不會啟動此進程,而且 null 會傳回。If the address of the executable file to start is a URL, the process is not started and null is returned.

此多載可讓您在不先建立新實例的情況下啟動進程 ProcessThis overload lets you start a process without first creating a new Process instance. 多載是建立新實例的明確步驟 Process 、設定 FileName 屬性的、、 Arguments UserNamePasswordDomain 屬性 StartInfo ,以及呼叫 Start Process 實例的替代方法。The overload is an alternative to the explicit steps of creating a new Process instance, setting the FileName, Arguments, UserName, Password, and Domain properties of the StartInfo property, and calling Start for the Process instance.

同樣地,如同 [執行] 對話方塊可以接受包含或不含 .exe 副檔名的可執行檔名稱一樣,.exe 副檔名在參數中是選擇性的。 fileNameSimilarly, in the same way that the Run dialog box can accept an executable file name with or without the .exe extension, the .exe extension is optional in the fileName parameter. 例如,您可以將參數設定 fileName 為「Notepad.exe」或「記事本」。For example, you can set the fileName parameter to either "Notepad.exe" or "Notepad". 如果 fileName 參數代表可執行檔,則 arguments 參數可能代表要處理的檔案,例如中的文字檔 Notepad.exe myfile.txtIf the fileName parameter represents an executable file, the arguments parameter might represent a file to act upon, such as the text file in Notepad.exe myfile.txt.

注意

檔案名必須代表 Start 具有 userNamepassword 和參數的多載中的可執行檔 domainThe file name must represent an executable file in the Start overloads that have userName, password, and domain parameters.

當您使用 Start 啟動處理常式時,可能需要關閉它,否則可能會遺失系統資源的風險。Whenever you use Start to start a process, you might need to close it or you risk losing system resources. 使用或關閉處理常式 CloseMainWindow KillClose processes using CloseMainWindow or Kill. 您可以使用屬性來檢查進程是否已關閉 HasExitedYou can check whether a process has already been closed by using its HasExited property.

適用於

Start(String, String, SecureString, String)

重要

此 API 不符合 CLS 規範。

藉由指定應用程式的名稱、使用者名稱、密碼和網域來啟動處理序資源,並將該資源與新的 Process 元件相關聯。Starts a process resource by specifying the name of an application, a user name, a password, and a domain and associates the resource with a new Process component.

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
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
Public Shared Function Start (fileName As String, userName As String, password As SecureString, domain As String) As Process

參數

fileName
String

要在處理序中執行之應用程式檔案的名稱。The name of an application file to run in the process.

userName
String

要在啟動處理序時使用的使用者名稱。The user name to use when starting the process.

password
SecureString

SecureString,包含啟動處理序時要使用的密碼。A SecureString that contains the password to use when starting the process.

domain
String

要在啟動處理序時使用的網域。The domain to use when starting the process.

傳回

Process

與處理程序資源關聯的新 Process,或者是 null (若未啟動任何處理程序資源)。A new Process that is associated with the process resource, or null if no process resource is started. 請注意,隨著已在執行中之相同處理程序啟動的新處理程序將獨立於其他處理程序之外。Note that a new process that's started alongside already running instances of the same process will be independent from the others. 此外,啟動可能會傳回非空值處理程序,且其 HasExited 屬性會設定為 trueIn addition, Start may return a non-null Process with its HasExited property already set to true. 在此案例中,已啟動的處理程序可能已啟用本身的現有執行個體並結束。In this case, the started process may have activated an existing instance of itself and then exited.

屬性

例外狀況

未指定檔案名稱。No file name was specified.

開啟關聯檔案時發生錯誤。There was an error in opening the associated file.

-或--or-

找不到在 fileName 中指定的檔案。The file specified in the fileName could not be found.

處理序物件已經處置。The process object has already been disposed.

Linux 或 macOS 不支援此成員 (僅限 .NET Core)。This member is not supported on Linux or macOS (.NET Core only).

範例

下列程式碼範例會示範如何使用此多載來啟動可執行檔,也 Win32Exception 會示範當嘗試啟動與非執行檔相關聯的應用程式時擲回。The following code example shows the use of this overload to start an executable file and also demonstrates the throwing of a Win32Exception when an attempt is made to start an application associated with a nonexecutable file.

// 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

備註

您可以使用此多載來建立新的進程及其主要執行緒,其方式是指定其檔案名、使用者名稱、密碼和網域。Use this overload to create a new process and its primary thread by specifying its file name, user name, password, and domain. 然後,新的進程會在指定認證的安全性內容中執行指定的可執行檔 (使用者、網域和密碼) 。The new process then runs the specified executable file in the security context of the specified credentials (user, domain, and password).

注意

當可執行檔位於遠端磁片磁碟機時,您必須使用統一資源識別項 (URI) 來識別網路共用,而不是連結的磁碟機號。When the executable file is located on a remote drive, you must identify the network share by using a uniform resource identifier (URI), not a linked drive letter.

注意

如果要啟動之可執行檔的位址是 URL,則不會啟動此進程,而且 null 會傳回。If the address of the executable file to start is a URL, the process is not started and null is returned.

此多載可讓您在不先建立新實例的情況下啟動進程 ProcessThis overload lets you start a process without first creating a new Process instance. 多載是建立新實例的明確步驟 Process 、設定 FileName 屬性的、、 UserName PasswordDomain 屬性 StartInfo ,以及呼叫 Start Process 實例的替代方法。The overload is an alternative to the explicit steps of creating a new Process instance, setting the FileName, UserName, Password, and Domain properties of the StartInfo property, and calling Start for the Process instance.

同樣地,如同 [執行] 對話方塊可以接受包含或不含 .exe 副檔名的可執行檔名稱一樣,.exe 副檔名在參數中是選擇性的。 fileNameSimilarly, in the same way that the Run dialog box can accept an executable file name with or without the .exe extension, the .exe extension is optional in the fileName parameter. 例如,您可以將參數設定 fileName 為「Notepad.exe」或「記事本」。For example, you can set the fileName parameter to either "Notepad.exe" or "Notepad". 如果 fileName 參數代表可執行檔,則 arguments 參數可能代表要處理的檔案,例如中的文字檔 Notepad.exe myfile.txtIf the fileName parameter represents an executable file, the arguments parameter might represent a file to act upon, such as the text file in Notepad.exe myfile.txt.

注意

檔案名必須代表 Start 具有 userNamepassword 和參數的多載中的可執行檔 domainThe file name must represent an executable file in the Start overloads that have userName, password, and domain parameters.

當您使用 Start 啟動處理常式時,可能需要關閉它,否則可能會遺失系統資源的風險。Whenever you use Start to start a process, you might need to close it or you risk losing system resources. 使用或關閉處理常式 CloseMainWindow KillClose processes using CloseMainWindow or Kill. 您可以使用屬性來檢查進程是否已關閉 HasExitedYou can check whether a process has already been closed by using its HasExited property.

適用於

Start(String, String)

藉由指定應用程式的名稱和一組命令列引數來啟動處理序資源,並將該資源與新的 Process 元件相關聯。Starts a process resource by specifying the name of an application and a set of command-line arguments, and associates the resource with a new Process component.

public:
 static System::Diagnostics::Process ^ Start(System::String ^ fileName, System::String ^ arguments);
public static System.Diagnostics.Process Start (string fileName, string arguments);
static member Start : string * string -> System.Diagnostics.Process
Public Shared Function Start (fileName As String, arguments As String) As Process

參數

fileName
String

要在處理序中執行之應用程式檔案的名稱。The name of an application file to run in the process.

arguments
String

啟動處理程序時要傳遞的命令列引數。Command-line arguments to pass when starting the process.

傳回

Process

與處理程序資源關聯的新 Process,或者是 null (若未啟動任何處理程序資源)。A new Process that is associated with the process resource, or null if no process resource is started. 請注意,隨著已在執行中之相同處理程序啟動的新處理程序將獨立於其他處理程序之外。Note that a new process that's started alongside already running instances of the same process will be independent from the others. 此外,啟動可能會傳回非空值處理程序,且其 HasExited 屬性會設定為 trueIn addition, Start may return a non-null Process with its HasExited property already set to true. 在此案例中,已啟動的處理程序可能已啟用本身的現有執行個體並結束。In this case, the started process may have activated an existing instance of itself and then exited.

例外狀況

fileNamearguments 參數為 nullThe fileName or arguments parameter is null.

開啟關聯的檔案時發生錯誤。An error occurred when opening the associated file.

-或--or-

找不到在 fileName 中指定的檔案。The file specified in the fileName could not be found.

-或--or- 引數長度和處理序完整路徑長度的總和超過 2080。The sum of the length of the arguments and the length of the full path to the process exceeds 2080. 與這個例外狀況相關聯的錯誤訊息可以是下列其中之一:「傳到系統呼叫的資料區域太小。」The error message associated with this exception can be one of the following: "The data area passed to a system call is too small." 或「拒絕存取」。or "Access is denied."

處理序物件已經處置。The process object has already been disposed.

PATH 環境變數具有含引號的字串。The PATH environment variable has a string containing quotes.

範例

下列範例會先產生 Internet Explorer 的實例,並在瀏覽器中顯示 [我的最愛] 資料夾的內容。The following example first spawns an instance of Internet Explorer and displays the contents of the Favorites folder in the browser. 接著,它會啟動 Internet Explorer 的其他實例,並顯示一些特定的頁面或網站。It then starts some other instances of Internet Explorer and displays some specific pages or sites. 最後它會開始 Internet Explorer,並在流覽至特定網站時,將視窗最小化。Finally it starts Internet Explorer with the window being minimized while navigating to a specific site.

#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

備註

您可以使用此多載,藉由指定其檔案名和命令列引數來啟動處理常式資源。Use this overload to start a process resource by specifying its file name and command-line arguments. 多載會將資源與新的 Process 物件產生關聯。The overload associates the resource with a new Process object.

注意

如果要啟動之可執行檔的位址是 URL,則不會啟動此進程,而且 null 會傳回。If the address of the executable file to start is a URL, the process is not started and null is returned.

此多載可讓您在不先建立新實例的情況下啟動進程 ProcessThis overload lets you start a process without first creating a new Process instance. 多載是建立新 Process 實例、設定 FileName 屬性的和 Arguments 成員 StartInfo ,以及呼叫 Start Process 實例的明確步驟的替代方法。The overload is an alternative to the explicit steps of creating a new Process instance, setting the FileName and Arguments members of the StartInfo property, and calling Start for the Process instance.

藉由指定檔案名和引數來啟動進程,類似于在 Windows 功能表的對話方塊中輸入檔案名和命令列引數 Run StartStarting a process by specifying its file name and arguments is similar to typing the file name and command-line arguments in the Run dialog box of the Windows Start menu. 因此,檔案名不需要代表可執行檔。Therefore, the file name does not need to represent an executable file. 它可以是任何副檔名已與系統上安裝之應用程式相關聯的檔案類型。It can be of any file type for which the extension has been associated with an application installed on the system. 例如,如果您有相關聯的文字檔與編輯器(例如 [記事本])相關聯,或者如果您有與 word 處理工具(例如 Microsoft Word)相關聯的 .doc 檔案,則檔案名可以有 .txt 副檔名。For example the file name can have a .txt extension if you have associated text files with an editor, such as Notepad, or it can have a .doc if you have associated .doc files with a word processing tool, such as Microsoft Word. 同樣地,如同 Run 對話方塊可以接受包含或不含 .exe 副檔名的可執行檔名稱一樣,.exe 副檔名在參數中是選擇性的。 fileNameSimilarly, in the same way that the Run dialog box can accept an executable file name with or without the .exe extension, the .exe extension is optional in the fileName parameter. 例如,您可以將參數設定 fileName 為「Notepad.exe」或「記事本」。For example, you can set the fileName parameter to either "Notepad.exe" or "Notepad". 如果 fileName 參數代表可執行檔,則 arguments 參數可能代表要處理的檔案,例如中的文字檔 Notepad.exe myfile.txtIf the fileName parameter represents an executable file, the arguments parameter might represent a file to act upon, such as the text file in Notepad.exe myfile.txt. 如果 fileName 參數代表命令 ( .cmd) 檔, arguments 參數必須包含 " /c " 或 " /k " 引數,以指定命令視窗在完成後是否結束或保持不變。If the fileName parameter represents a command (.cmd) file, the arguments parameter must include either a "/c" or "/k" argument to specify whether the command window exits or remains after completion.

不同于其他多載,沒有參數的多載 Start 不是 static 成員。Unlike the other overloads, the overload of Start that has no parameters is not a static member. 當您已建立 Process 實例和指定的啟動資訊 (包括檔案名) ,而且您想要啟動處理常式資源並將其與現有的實例建立關聯時,請使用該多載 ProcessUse that overload when you have already created a Process instance and specified start information (including the file name), and you want to start a process resource and associate it with the existing Process instance. static當您想要建立新的 Process 元件,而不是啟動現有元件的處理常式時,請使用其中一個多載。Use one of the static overloads when you want to create a new Process component rather than start a process for an existing component. 這個多載和沒有參數的多載,可讓您指定要啟動的進程資源的檔案名,以及要傳遞的命令列引數。Both this overload and the overload that has no parameters allow you to specify the file name of the process resource to start and command-line arguments to pass.

如果您在系統中使用引號宣告了路徑變數,您必須在啟動任何在該位置找到的進程時,完整限定該路徑。If you have a path variable declared in your system using quotes, you must fully qualify that path when starting any process found in that location. 否則,系統將找不到路徑。Otherwise, the system will not find the path. 例如,如果 c:\mypath 不在您的路徑中,而且您使用引號來新增它: path = %path%;"c:\mypath" ,則必須在啟動時完整限定中的任何處理程式 c:\mypathFor example, if c:\mypath is not in your path, and you add it using quotation marks: path = %path%;"c:\mypath", you must fully qualify any process in c:\mypath when starting it.

注意

ASP.NET 網頁和伺服器控制項程式碼會在 Web 服務器上的 ASP.NET 背景工作進程內容中執行。ASP.NET Web page and server control code executes in the context of the ASP.NET worker process on the Web server. 如果您 Start 在 ASP.NET 網頁或伺服器控制項中使用方法,則新的進程會在具有限制許可權的 Web 服務器上執行。If you use the Start method in an ASP.NET Web page or server control, the new process executes on the Web server with restricted permissions. 此程式不會在與用戶端瀏覽器相同的內容中啟動,也不能存取使用者桌面。The process does not start in the same context as the client browser, and does not have access to the user desktop.

當您使用 Start 啟動處理常式時,可能需要關閉它,否則可能會遺失系統資源的風險。Whenever you use Start to start a process, you might need to close it or you risk losing system resources. 使用或關閉處理常式 CloseMainWindow KillClose processes using CloseMainWindow or Kill. 您可以使用屬性來檢查進程是否已關閉 HasExitedYou can check whether a process has already been closed by using its HasExited property.

您必須注意受控執行緒中的單元狀態。A note about apartment states in managed threads is necessary here. UseShellExecute true 在處理元件的屬性上時 StartInfo ,請確定您已在此方法上設定屬性,以在應用程式上設定執行緒模型 [STAThread] main()When UseShellExecute is true on the process component's StartInfo property, make sure you have set a threading model on your application by setting the attribute [STAThread] on the main() method. 否則,受管理的執行緒可以處於 unknown 狀態,或處於 MTA 狀態,後者會與衝突 UseShellExecute trueOtherwise, a managed thread can be in an unknown state or put in the MTA state, the latter of which conflicts with UseShellExecute being true. 某些方法需要單元狀態不是 unknownSome methods require that the apartment state not be unknown. 如果未明確設定狀態,則當應用程式遇到這類方法時,它會預設為 MTA ,而且在設定之後,就無法變更單元狀態。If the state is not explicitly set, when the application encounters such a method, it defaults to MTA, and once set, the apartment state cannot be changed. 不過, MTA 當作業系統 shell 正在管理執行緒時,會導致擲回例外狀況。However, MTA causes an exception to be thrown when the operating system shell is managing the thread.

另請參閱

適用於

Start()

啟動 (或重複使用) 這個 Process 元件的 StartInfo 屬性指定的處理序資源,並將其與元件相關聯。Starts (or reuses) the process resource that is specified by the StartInfo property of this Process component and associates it with the component.

public:
 bool Start();
public bool Start ();
member this.Start : unit -> bool
Public Function Start () As Boolean

傳回

Boolean

如果啟動處理序資源,則為 true;如果沒有啟動任何新的處理序資源 (例如,如果重複使用現有的處理序),則為 falsetrue if a process resource is started; false if no new process resource is started (for example, if an existing process is reused).

例外狀況

Process 元件的 StartInfo 中未指定檔案名稱。No file name was specified in the Process component's StartInfo.

-或--or-

UseShellExecute 屬性的 StartInfo 成員是 true ,而 RedirectStandardInputRedirectStandardOutputRedirectStandardErrortrueThe UseShellExecute member of the StartInfo property is true while RedirectStandardInput, RedirectStandardOutput, or RedirectStandardError is true.

開啟關聯檔案時發生錯誤。There was an error in opening the associated file.

處理序物件已經處置。The process object has already been disposed.

此方法在沒有殼層支援的作業系統上不受支援,例如 Nano 伺服器 (僅限 .NET Core)。Method not supported on operating systems without shell support such as Nano Server (.NET Core only).

範例

下列範例會使用類別的實例 Process 來啟動處理常式。The following example uses an instance of the Process class to start a 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 is 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 is 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 is 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

備註

使用此多載來啟動進程資源,並將它與目前的元件產生關聯 ProcessUse this overload to start a process resource and associate it with the current Process component. 傳回值 true 指出已啟動新的進程資源。The return value true indicates that a new process resource was started. 如果屬性(property)成員所指定的處理常式資源 FileName StartInfo 已在電腦上執行,則不會啟動任何額外的進程資源。If the process resource specified by the FileName member of the StartInfo property is already running on the computer, no additional process resource is started. 相反地,會重複使用正在執行的進程資源,並 false 傳回。Instead, the running process resource is reused and false is returned.

您可以藉由指定位置來啟動 ClickOnce 應用程式 (例如,您原本安裝應用程式的網址) 。You can start a ClickOnce application by specifying the location (for example, a Web address) from which you originally installed the application. 請勿在您的硬碟上指定已安裝的位置來啟動 ClickOnce 應用程式。Do not start a ClickOnce application by specifying its installed location on your hard drive.

注意

如果您使用 Visual Studio,方法的這個多載 Start 是您將元件拖曳至設計工具之後,要插入程式碼中的多載 ProcessIf you are using Visual Studio, this overload of the Start method is the one that you insert into your code after you drag a Process component onto the designer. 使用 Properties 視窗展開 StartInfo 類別目錄,並將適當的值寫入 FileName 屬性。Use the Properties window to expand the StartInfo category and write the appropriate value into the FileName property. 您的變更將會以表單的程式顯示 InitializeComponentYour changes will appear in the form's InitializeComponent procedure.

的這個多載 Start 不是 static 方法。This overload of Start is not a static method. 您必須從類別的實例呼叫它 ProcessYou must call it from an instance of the Process class. 在呼叫之前 Start ,您必須先指定 StartInfo 此實例的屬性資訊 Process ,因為這項資訊會用來判斷要啟動的進程資源。Before calling Start, you must first specify StartInfo property information for this Process instance, because that information is used to determine the process resource to start.

方法的其他多載 Startstatic 成員。The other overloads of the Start method are static members. 在呼叫方法的多載之前,您不需要建立元件的實例 ProcessYou do not need to create an instance of the Process component before you call those overloads of the method. 相反地,您可以呼叫 Start Process 類別本身,並在 Process 進程啟動時建立新的元件。Instead, you can call Start for the Process class itself, and a new Process component is created if the process was started. 或者, null 如果已重複使用進程,就會傳回。Or, null is returned if a process was reused. 進程資源會自動與方法所傳回的新元件產生關聯 Process StartThe process resource is automatically associated with the new Process component that is returned by the Start method.

StartInfo成員可以用來複製 Run Windows 功能表之對話方塊的功能 StartThe StartInfo members can be used to duplicate the functionality of the Run dialog box of the Windows Start menu. 可以在命令列中輸入的任何內容,都可以藉由在屬性中設定適當的值來啟動 StartInfoAnything that can be typed into a command line can be started by setting the appropriate values in the StartInfo property. 唯一 StartInfo 必須設定的屬性是 FileName 屬性。The only StartInfo property that must be set is the FileName property. FileName屬性不一定要是可執行檔。The FileName property does not have to be an executable file. 它可以是任何副檔名已與系統上安裝之應用程式相關聯的檔案類型。It can be of any file type for which the extension has been associated with an application that is installed on the system. 例如, FileName 如果您有相關聯的文字檔與編輯器(例如 [記事本])相關聯,或者如果您有與 word 處理工具(例如 Microsoft word)相關聯的 .doc 檔案,則屬性的副檔名可以是 .txt。For example, the FileName property can have a .txt extension if you have associated text files with an editor, such as Notepad, or it can have a .doc extension if you have associated .doc files with a word processing tool, such as Microsoft Word.

在命令列中,您可以指定要對特定檔案類型採取的動作。In the command line, you can specify actions to take for certain types of files. 例如,您可以列印檔案或編輯文字檔。For example, you can print documents or edit text files. 請使用屬性的成員來指定這些動作 Verb StartInfoSpecify these actions using the Verb member of the StartInfo property. 針對其他類型的檔案,當您從對話方塊啟動檔案時,可以指定命令列引數 RunFor other types of files, you can specify command-line arguments when you start the file from the Run dialog box. 例如,如果您將瀏覽器指定為,您可以傳遞 URL 作為引數 FileNameFor example, you can pass a URL as an argument if you specify your browser as the FileName. 您可以在屬性的成員中指定這些引數 StartInfo ArgumentsThese arguments can be specified in the StartInfo property's Arguments member.

如果您在系統中使用引號宣告了路徑變數,您必須在啟動任何在該位置找到的進程時,完整限定該路徑。If you have a path variable declared in your system using quotes, you must fully qualify that path when starting any process found in that location. 否則,系統將找不到路徑。Otherwise, the system will not find the path. 例如,如果 c:\mypath 不在您的路徑中,而且您使用引號來新增它: path = %path%;"c:\mypath" ,則必須在啟動時完整限定中的任何處理程式 c:\mypathFor example, if c:\mypath is not in your path, and you add it using quotation marks: path = %path%;"c:\mypath", you must fully qualify any process in c:\mypath when starting it.

注意

ASP.NET 網頁和伺服器控制項程式碼會在 Web 服務器上的 ASP.NET 背景工作進程內容中執行。ASP.NET Web page and server control code executes in the context of the ASP.NET worker process on the Web server. 如果您 Start 在 ASP.NET 網頁或伺服器控制項中使用方法,則新的進程會在具有限制許可權的 Web 服務器上執行。If you use the Start method in an ASP.NET Web page or server control, the new process executes on the Web server with restricted permissions. 此程式不會在與用戶端瀏覽器相同的內容中啟動,也不能存取使用者桌面。The process does not start in the same context as the client browser, and does not have access to the user desktop.

當您使用 Start 啟動處理常式時,可能需要關閉它,否則可能會遺失系統資源的風險。Whenever you use Start to start a process, you might need to close it or you risk losing system resources. 使用或關閉處理常式 CloseMainWindow KillClose processes using CloseMainWindow or Kill. 您可以使用屬性來檢查進程是否已關閉 HasExitedYou can check whether a process has already been closed by using its HasExited property.

您必須注意受控執行緒中的單元狀態。A note about apartment states in managed threads is necessary here. UseShellExecute true 在處理元件的屬性上時 StartInfo ,請確定您已在此方法上設定屬性,以在應用程式上設定執行緒模型 [STAThread] main()When UseShellExecute is true on the process component's StartInfo property, make sure you have set a threading model on your application by setting the attribute [STAThread] on the main() method. 否則,受管理的執行緒可以處於 unknown 狀態,或處於 MTA 狀態,後者會與衝突 UseShellExecute trueOtherwise, a managed thread can be in an unknown state or put in the MTA state, the latter of which conflicts with UseShellExecute being true. 某些方法需要單元狀態不是 unknownSome methods require that the apartment state not be unknown. 如果未明確設定狀態,則當應用程式遇到這類方法時,它會預設為 MTA ,而且在設定之後,就無法變更單元狀態。If the state is not explicitly set, when the application encounters such a method, it defaults to MTA, and once set, the apartment state cannot be changed. 不過, MTA 當作業系統 shell 正在管理執行緒時,會導致擲回例外狀況。However, MTA causes an exception to be thrown when the operating system shell is managing the thread.

另請參閱

適用於

Start(String)

藉由指定文件或應用程式檔案的名稱啟動處理序資源,並將該資源與新的 Process 元件相關聯。Starts a process resource by specifying the name of a document or application file and associates the resource with a new Process component.

public:
 static System::Diagnostics::Process ^ Start(System::String ^ fileName);
public static System.Diagnostics.Process Start (string fileName);
static member Start : string -> System.Diagnostics.Process
Public Shared Function Start (fileName As String) As Process

參數

fileName
String

要在處理序中執行之文件或應用程式檔案的名稱。The name of a document or application file to run in the process.

傳回

Process

與處理程序資源關聯的新 Process,或者是 null (若未啟動任何處理程序資源)。A new Process that is associated with the process resource, or null if no process resource is started. 請注意,隨著已在執行中之相同處理程序啟動的新處理程序將獨立於其他處理程序之外。Note that a new process that's started alongside already running instances of the same process will be independent from the others. 此外,啟動可能會傳回非空值處理程序,且其 HasExited 屬性會設定為 trueIn addition, Start may return a non-null Process with its HasExited property already set to true. 在此案例中,已啟動的處理程序可能已啟用本身的現有執行個體並結束。In this case, the started process may have activated an existing instance of itself and then exited.

例外狀況

開啟關聯的檔案時發生錯誤。An error occurred when opening the associated file.

-或--or-

找不到在 fileName 中指定的檔案。The file specified in the fileName could not be found.

處理序物件已經處置。The process object has already been disposed.

PATH 環境變數具有含引號的字串。The PATH environment variable has a string containing quotes.

範例

下列範例會先產生 Internet Explorer 的實例,並在瀏覽器中顯示 [我的最愛] 資料夾的內容。The following example first spawns an instance of Internet Explorer and displays the contents of the Favorites folder in the browser. 接著,它會啟動 Internet Explorer 的其他實例,並顯示一些特定的頁面或網站。It then starts some other instances of Internet Explorer and displays some specific pages or sites. 最後它會開始 Internet Explorer,並在流覽至特定網站時,將視窗最小化。Finally it starts Internet Explorer with the window being minimized while navigating to a specific site.

#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

備註

您可以使用此多載,藉由指定檔案的檔案名來啟動處理常式資源。Use this overload to start a process resource by specifying its file name. 多載會將資源與新的 Process 物件產生關聯。The overload associates the resource with a new Process object.

注意

如果要啟動之可執行檔的位址是 URL,則不會啟動此進程,而且 null 會傳回。If the address of the executable file to start is a URL, the process is not started and null is returned.

此多載可讓您在不先建立新實例的情況下啟動進程 ProcessThis overload lets you start a process without first creating a new Process instance. 多載是建立新 Process 實例、設定 FileName StartInfo 屬性成員及呼叫 Start Process 實例的明確步驟的替代方法。The overload is an alternative to the explicit steps of creating a new Process instance, setting the FileName member of the StartInfo property, and calling Start for the Process instance.

您可以藉由將參數設定為位置來啟動 ClickOnce 應用程式 fileName (例如,您原本用來安裝應用程式的網址) 。You can start a ClickOnce application by setting the fileName parameter to the location (for example, a Web address) from which you originally installed the application. 請勿在您的硬碟上指定已安裝的位置來啟動 ClickOnce 應用程式。Do not start a ClickOnce application by specifying its installed location on your hard drive.

藉由指定其檔案名來啟動進程,類似于在 Windows 功能表的對話方塊中輸入資訊 Run StartStarting a process by specifying its file name is similar to typing the information in the Run dialog box of the Windows Start menu. 因此,檔案名不需要代表可執行檔。Therefore, the file name does not need to represent an executable file. 它可以是任何副檔名已與系統上安裝之應用程式相關聯的檔案類型。It can be of any file type for which the extension has been associated with an application installed on the system. 例如,如果您有相關聯的文字檔與編輯器(例如 [記事本])相關聯,或者如果您有與 word 處理工具(例如 Microsoft Word)相關聯的 .doc 檔案,則檔案名可以有 .txt 副檔名。For example the file name can have a .txt extension if you have associated text files with an editor, such as Notepad, or it can have a .doc if you have associated .doc files with a word processing tool, such as Microsoft Word. 同樣地,如同 Run 對話方塊可以接受包含或不含 .exe 副檔名的可執行檔名稱一樣,.exe 副檔名在參數中是選擇性的。 fileNameSimilarly, in the same way that the Run dialog box can accept an executable file name with or without the .exe extension, the .exe extension is optional in the fileName parameter. 例如,您可以將參數設定 fileName 為「Notepad.exe」或「記事本」。For example, you can set the fileName parameter to either "Notepad.exe" or "Notepad".

此多載不允許處理常式的命令列引數。This overload does not allow command-line arguments for the process. 如果您需要為處理常式指定一或多個命令列引數,請使用 Process.Start(ProcessStartInfo) 或多載 Process.Start(String, String)If you need to specify one or more command-line arguments for the process, use the Process.Start(ProcessStartInfo) or Process.Start(String, String) overloads.

不同于其他多載,沒有參數的多載 Start 不是 static 成員。Unlike the other overloads, the overload of Start that has no parameters is not a static member. 當您已建立 Process 實例和指定的啟動資訊 (包括檔案名) ,而且您想要啟動處理常式資源並將其與現有的實例建立關聯時,請使用該多載 ProcessUse that overload when you have already created a Process instance and specified start information (including the file name), and you want to start a process resource and associate it with the existing Process instance. static當您想要建立新的 Process 元件,而不是啟動現有元件的處理常式時,請使用其中一個多載。Use one of the static overloads when you want to create a new Process component rather than start a process for an existing component. 這個多載和沒有參數的多載,可讓您指定要啟動之進程資源的檔案名。Both this overload and the overload that has no parameters allow you to specify the file name of the process resource to start.

如果您在系統中使用引號宣告了路徑變數,您必須在啟動任何在該位置找到的進程時,完整限定該路徑。If you have a path variable declared in your system using quotes, you must fully qualify that path when starting any process found in that location. 否則,系統將找不到路徑。Otherwise, the system will not find the path. 例如,如果 c:\mypath 不在您的路徑中,而且您使用引號來新增它: path = %path%;"c:\mypath" ,則必須在啟動時完整限定中的任何處理程式 c:\mypathFor example, if c:\mypath is not in your path, and you add it using quotation marks: path = %path%;"c:\mypath", you must fully qualify any process in c:\mypath when starting it.

注意

ASP.NET 網頁和伺服器控制項程式碼會在 Web 服務器上的 ASP.NET 背景工作進程內容中執行。ASP.NET Web page and server control code executes in the context of the ASP.NET worker process on the Web server. 如果您 Start 在 ASP.NET 網頁或伺服器控制項中使用方法,則新的進程會在具有限制許可權的 Web 服務器上執行。If you use the Start method in an ASP.NET Web page or server control, the new process executes on the Web server with restricted permissions. 此程式不會在與用戶端瀏覽器相同的內容中啟動,也不能存取使用者桌面。The process does not start in the same context as the client browser, and does not have access to the user desktop.

當您使用 Start 啟動處理常式時,可能需要關閉它,否則可能會遺失系統資源的風險。Whenever you use Start to start a process, you might need to close it or you risk losing system resources. 使用或關閉處理常式 CloseMainWindow KillClose processes using CloseMainWindow or Kill. 您可以使用屬性來檢查進程是否已關閉 HasExitedYou can check whether a process has already been closed by using its HasExited property.

您必須注意受控執行緒中的單元狀態。A note about apartment states in managed threads is necessary here. UseShellExecute true 在處理元件的屬性上時 StartInfo ,請確定您已在此方法上設定屬性,以在應用程式上設定執行緒模型 [STAThread] main()When UseShellExecute is true on the process component's StartInfo property, make sure you have set a threading model on your application by setting the attribute [STAThread] on the main() method. 否則,受管理的執行緒可以處於 unknown 狀態,或處於 MTA 狀態,後者會與衝突 UseShellExecute trueOtherwise, a managed thread can be in an unknown state or put in the MTA state, the latter of which conflicts with UseShellExecute being true. 某些方法需要單元狀態不是 unknownSome methods require that the apartment state not be unknown. 如果未明確設定狀態,則當應用程式遇到這類方法時,它會預設為 MTA ,而且在設定之後,就無法變更單元狀態。If the state is not explicitly set, when the application encounters such a method, it defaults to MTA, and once set, the apartment state cannot be changed. 不過, MTA 當作業系統 shell 正在管理執行緒時,會導致擲回例外狀況。However, MTA causes an exception to be thrown when the operating system shell is managing the thread.

另請參閱

適用於

Start(ProcessStartInfo)

啟動含有處理序啟動資訊 (例如,要啟動之處理序的檔名) 的參數所指定的處理序資源,並將該資源與新的 Process 元件相關聯。Starts the process resource that is specified by the parameter containing process start information (for example, the file name of the process to start) and associates the resource with a new Process component.

public:
 static System::Diagnostics::Process ^ Start(System::Diagnostics::ProcessStartInfo ^ startInfo);
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
Public Shared Function Start (startInfo As ProcessStartInfo) As Process

參數

startInfo
ProcessStartInfo

ProcessStartInfo,包含用來啟動處理序的資訊,包括檔名和任何命令列引數。The ProcessStartInfo that contains the information that is used to start the process, including the file name and any command-line arguments.

傳回

Process

與處理程序資源關聯的新 Process,或者是 null (若未啟動任何處理程序資源)。A new Process that is associated with the process resource, or null if no process resource is started. 請注意,隨著已在執行中之相同處理程序啟動的新處理程序將獨立於其他處理程序之外。Note that a new process that's started alongside already running instances of the same process will be independent from the others. 此外,啟動可能會傳回非空值處理程序,且其 HasExited 屬性會設定為 trueIn addition, Start may return a non-null Process with its HasExited property already set to true. 在此案例中,已啟動的處理程序可能已啟用本身的現有執行個體並結束。In this case, the started process may have activated an existing instance of itself and then exited.

例外狀況

startInfo 參數的 FileName 屬性沒有指定任何檔案名稱。No file name was specified in the startInfo parameter's FileName property.

-或--or- startInfo 參數的 UseShellExecute 屬性是 true,而 RedirectStandardInputRedirectStandardOutputRedirectStandardError 屬性也是 trueThe UseShellExecute property of the startInfo parameter is true and the RedirectStandardInput, RedirectStandardOutput, or RedirectStandardError property is also true.

-或--or- startInfo 參數的 UseShellExecute 屬性是 true,而 UserName 屬性不是 null 或空白,或者 Password 屬性不是 nullThe UseShellExecute property of the startInfo parameter is true and the UserName property is not null or empty or the Password property is not null.

startInfo 參數為 nullThe startInfo parameter is null.

處理序物件已經處置。The process object has already been disposed.

開啟關聯的檔案時發生錯誤。An error occurred when opening the associated file.

-或--or- 找不到 startInfo 參數的 FileName 屬性中所指定的檔案。The file specified in the startInfo parameter's FileName property could not be found.

-或--or- 引數長度和處理序完整路徑長度的總和超過 2080。The sum of the length of the arguments and the length of the full path to the process exceeds 2080. 與這個例外狀況相關聯的錯誤訊息可以是下列其中之一:「傳到系統呼叫的資料區域太小。」The error message associated with this exception can be one of the following: "The data area passed to a system call is too small." 或「拒絕存取」。or "Access is denied."

此方法在沒有殼層支援的作業系統上不受支援,例如 Nano 伺服器 (僅限 .NET Core)。Method not supported on operating systems without shell support such as Nano Server (.NET Core only).

範例

下列範例會先產生 Internet Explorer 的實例,並在瀏覽器中顯示 [我的最愛] 資料夾的內容。The following example first spawns an instance of Internet Explorer and displays the contents of the Favorites folder in the browser. 接著,它會啟動 Internet Explorer 的其他實例,並顯示一些特定的頁面或網站。It then starts some other instances of Internet Explorer and displays some specific pages or sites. 最後它會開始 Internet Explorer,並在流覽至特定網站時,將視窗最小化。Finally it starts Internet Explorer with the window being minimized while navigating to a specific site.

如需此方法其他用途的其他範例,請參閱類別的個別屬性 ProcessStartInfoFor additional examples of other uses of this method, refer to the individual properties of the ProcessStartInfo class.

#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

備註

您可以使用此多載,藉由指定實例來啟動處理常式資源 ProcessStartInfoUse this overload to start a process resource by specifying a ProcessStartInfo instance. 多載會將資源與新的 Process 物件產生關聯。The overload associates the resource with a new Process object.

注意

如果要啟動之可執行檔的位址是 URL,則不會啟動此進程,而且 null 會傳回。If the address of the executable file to start is a URL, the process is not started and null is returned.

此多載可讓您在不先建立新實例的情況下啟動進程 ProcessThis overload lets you start a process without first creating a new Process instance. 搭配參數使用此多載 ProcessStartInfo ,是建立新 Process 實例、設定其 StartInfo 屬性,以及呼叫 Start 實例的明確步驟的替代方法 ProcessUsing this overload with a ProcessStartInfo parameter is an alternative to the explicit steps of creating a new Process instance, setting its StartInfo properties, and calling Start for the Process instance.

使用 ProcessStartInfo 實例作為參數,可讓您對 Start 傳遞至呼叫以啟動處理常式的最大控制權進行呼叫。Using a ProcessStartInfo instance as the parameter lets you call Start with the most control over what is passed into the call to start the process. 如果您只需要傳遞檔案名或檔案名和引數,則不需要建立新的 ProcessStartInfo 實例,但這是一個選項。If you need to pass only a file name or a file name and arguments, it is not necessary to create a new ProcessStartInfo instance, although that is an option. 唯一 Process.StartInfo 必須設定的屬性是 FileName 屬性。The only Process.StartInfo property that must be set is the FileName property. FileName屬性不需要表示可執行檔。The FileName property does not need to represent an executable file. 它可以是任何副檔名已與系統上安裝之應用程式相關聯的檔案類型。It can be of any file type for which the extension has been associated with an application that is installed on the system. 例如, FileName 如果您有相關聯的文字檔與編輯器(例如 [記事本])相關聯,或者如果您有與 word 處理工具(例如 Microsoft word)相關聯的 .doc 檔案,則屬性的副檔名可以是 .txt。For example, the FileName property can have a .txt extension if you have associated text files with an editor, such as Notepad, or it can have a .doc extension if you have associated .doc files with a word processing tool, such as Microsoft Word.

您可以藉由指定位置來啟動 ClickOnce 應用程式 (例如,您原本安裝應用程式的網址) 。You can start a ClickOnce application by specifying the location (for example, a Web address) from which you originally installed the application. 請勿在您的硬碟上指定已安裝的位置來啟動 ClickOnce 應用程式。Do not start a ClickOnce application by specifying its installed location on your hard drive.

如果 ProcessStartInfo.UserName ProcessStartInfo.Password 設定實例的和屬性 StartInfo ,則會呼叫非受控函式 CreateProcessWithLogonW ,這會在新視窗中啟動進程,即使 ProcessStartInfo.CreateNoWindow 屬性值為 trueProcessStartInfo.WindowStyle 屬性值為 ProcessWindowStyle.HiddenIf the ProcessStartInfo.UserName and ProcessStartInfo.Password properties of the StartInfo instance are set, the unmanaged CreateProcessWithLogonW function is called, which starts the process in a new window even if the ProcessStartInfo.CreateNoWindow property value is true or the ProcessStartInfo.WindowStyle property value is ProcessWindowStyle.Hidden. 如果 ProcessStartInfo.Domain 屬性為 null ,則 ProcessStartInfo.UserName 屬性必須是 UPN 格式,也就是 使用者 @ DNS_domain_nameIf the ProcessStartInfo.Domain property is null, the ProcessStartInfo.UserName property must be in UPN format, user@DNS_domain_name.

不同于其他多載,沒有參數的多載 Start 不是 static 成員。Unlike the other overloads, the overload of Start that has no parameters is not a static member. 當您已建立 Process 實例和指定的啟動資訊 (包括檔案名) ,而且您想要啟動處理常式資源並將其與現有的實例建立關聯時,請使用該多載 ProcessUse that overload when you have already created a Process instance and specified start information (including the file name), and you want to start a process resource and associate it with the existing Process instance. static當您想要建立新的 Process 元件,而不是啟動現有元件的處理常式時,請使用其中一個多載。Use one of the static overloads when you want to create a new Process component rather than start a process for an existing component. 這個多載和沒有參數的多載,可讓您使用實例來指定進程資源的啟動資訊 ProcessStartInfoBoth this overload and the overload that has no parameters allow you to specify the start information for the process resource by using a ProcessStartInfo instance.

如果您在系統中使用引號宣告了路徑變數,您必須在啟動任何在該位置找到的進程時,完整限定該路徑。If you have a path variable declared in your system using quotes, you must fully qualify that path when starting any process found in that location. 否則,系統將找不到路徑。Otherwise, the system will not find the path. 例如,如果 c:\mypath 不在您的路徑中,而且您使用引號來新增它: path = %path%;"c:\mypath" ,則必須在啟動時完整限定中的任何處理程式 c:\mypathFor example, if c:\mypath is not in your path, and you add it using quotation marks: path = %path%;"c:\mypath", you must fully qualify any process in c:\mypath when starting it.

注意

ASP.NET 網頁和伺服器控制項程式碼會在 Web 服務器上的 ASP.NET 背景工作進程內容中執行。ASP.NET Web page and server control code executes in the context of the ASP.NET worker process on the Web server. 如果您 Start 在 ASP.NET 網頁或伺服器控制項中使用方法,則新的進程會在具有限制許可權的 Web 服務器上執行。If you use the Start method in an ASP.NET Web page or server control, the new process executes on the Web server with restricted permissions. 此程式不會在與用戶端瀏覽器相同的內容中啟動,也不能存取使用者桌面。The process does not start in the same context as the client browser, and does not have access to the user desktop.

當您使用 Start 啟動處理常式時,可能需要關閉它,否則可能會遺失系統資源的風險。Whenever you use Start to start a process, you might need to close it or you risk losing system resources. 使用或關閉處理常式 CloseMainWindow KillClose processes using CloseMainWindow or Kill. 您可以使用屬性來檢查進程是否已關閉 HasExitedYou can check whether a process has already been closed by using its HasExited property.

您必須注意受控執行緒中的單元狀態。A note about apartment states in managed threads is necessary here. UseShellExecute 位於 true 參數上時 startInfo ,請確定您已在該方法上設定屬性,以在應用程式上設定執行緒模型 [STAThread] main()When UseShellExecute is true on the startInfo parameter, make sure you have set a threading model on your application by setting the attribute [STAThread] on the main() method. 否則,受管理的執行緒可以處於 unknown 狀態,或處於 MTA 狀態,後者會與衝突 UseShellExecute trueOtherwise, a managed thread can be in an unknown state or put in the MTA state, the latter of which conflicts with UseShellExecute being true. 某些方法需要單元狀態不是 unknownSome methods require that the apartment state not be unknown. 如果未明確設定狀態,則當應用程式遇到這類方法時,它會預設為 MTA ,而且在設定之後,就無法變更單元狀態。If the state is not explicitly set, when the application encounters such a method, it defaults to MTA, and once set, the apartment state cannot be changed. 不過, MTA 當作業系統 shell 正在管理執行緒時,會導致擲回例外狀況。However, MTA causes an exception to be thrown when the operating system shell is managing the thread.

另請參閱

適用於

Start(String, IEnumerable<String>)

藉由指定應用程式的名稱和一組命令列引數來啟動處理序資源。Starts a process resource by specifying the name of an application and a set of command line arguments.

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);
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

要在處理序中執行之文件或應用程式檔案的名稱。The name of a document or application file to run in the process.

arguments
IEnumerable<String>

啟動處理序時要傳遞的命令列引數。The command-line arguments to pass when starting the process.

傳回

Process

與處理程序資源關聯的新 Process,或者是 null (若未啟動任何處理程序資源)。A new Process that is associated with the process resource, or null if no process resource is started.

備註

必要時,會自動將每個引數進行轉義。Each argument will be escaped automatically if required.

適用於