Process.Start Process.Start Process.Start Process.Start Method

定義

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

多載

Start() Start() Start() 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(ProcessStartInfo) Start(ProcessStartInfo) Start(ProcessStartInfo) 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) Start(String) Start(String) 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(String, String) Start(String, String) Start(String, String) 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(String, String, SecureString, String) Start(String, String, SecureString, String) Start(String, String, SecureString, String) 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, String, SecureString, String) Start(String, String, String, SecureString, String) Start(String, String, String, SecureString, String) 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() Start() Start() 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

傳回

如果啟動處理序資源,則為 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. 或者,StartInfo 屬性的 UseShellExecute 成員是 true,而 RedirectStandardInputRedirectStandardOutputRedirectStandardErrortrue-or- The 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
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 'Main
    End Class
End Namespace

備註

使用此多載來啟動處理序資源,並將它與目前的關聯Process元件。Use 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. 如果所指定的處理序資源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.

您可以藉由指定從中您原先安裝應用程式的位置 (例如,Web 位址) 來啟動 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方法就是拖曳後,您將您的程式碼插入Process元件拖曳至設計工具。If 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. 您的變更會出現在表單的InitializeComponent程序。Your changes will appear in the form's InitializeComponent procedure.

這個多載Start不是static方法。This overload of Start is not a static method. 您必須呼叫它的執行個體從Process類別。You 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.

其他多載Start方法會static成員。The other overloads of the Start method are static members. 您不需要建立的執行個體Process元件才能呼叫方法的多載。You do not need to create an instance of the Process component before you call those overloads of the method. 相反地,您可以呼叫StartforProcess類別本身,以及新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所傳回的元件Start方法。The process resource is automatically associated with the new Process component that is returned by the Start method.

StartInfo成員可以用來複製的功能Run對話方塊中的 WindowsStart功能表。The StartInfo members can be used to duplicate the functionality of the Run dialog box of the Windows Start menu. 任何可以輸入命令列的項目可以啟動程式中設定適當的值StartInfo屬性。Anything 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屬性可以有副檔名為.txt,如果您的文字檔案關聯的編輯器,例如 [記事本],或如果您有使用文字處理工具,例如 Microsoft Word associated.doc 檔案,它可以有副檔名為.doc。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隸屬StartInfo屬性。Specify these actions using the Verb member of the StartInfo property. 對於其他類型的檔案中,您可以指定命令列引數時開始從檔案Run 對話方塊。For 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屬性的Arguments成員。These 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:\mypath時啟動它。For 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 Web 網頁和伺服器控制項的程式碼。ASP.NET Web page and server control code executes in the context of the ASP.NET worker process on the Web server. 如果您使用StartASP.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. 關閉使用的處理程序CloseMainWindowKillClose processes using CloseMainWindow or Kill. 您可以檢查處理程序是否已經使用關閉其HasExited屬性。You can check whether a process has already been closed by using its HasExited property.

這裡的附註中的 managed 執行緒的 apartment 狀態是必要的。A note about apartment states in managed threads is necessary here. UseShellExecutetrue處理序元件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. Managed 的執行緒在可以的否則為unknown狀態,或放入MTA狀態,後者的 je v konfliktuUseShellExecute正在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. 某些方法需要的 apartment 狀態不是unknownSome methods require that the apartment state not be unknown. 如果狀態未明確設定,當應用程式遇到這種方法,則預設為MTA,而且設定之後,apartment 狀態無法變更。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造成作業系統殼層來管理執行緒時擲回例外狀況。However, MTA causes an exception to be thrown when the operating system shell is managing the thread.

安全性

LinkDemand
針對完全信任立即呼叫端。for full trust for the immediate caller. 這個成員無法供部分信任的程式碼使用。This member cannot be used by partially trusted code.

另請參閱

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

參數

startInfo
ProcessStartInfo ProcessStartInfo ProcessStartInfo 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,或者是 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.

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

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

-或--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.

這個方法的其他用途的其他範例,請參閱個別屬性的ProcessStartInfo類別。For 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()
{
    // 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 = 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 'OpenApplication

        ' Opens urls and .html documents using Internet Explorer.
        Sub 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")
        End Sub 'OpenWithArguments

        ' 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 'OpenWithStartInfo

        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 'Main
    End Class 'MyProcess
End Namespace 'MyProcessSample

備註

使用此多載來啟動處理序資源,藉由指定ProcessStartInfo執行個體。Use 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.

這個多載可讓您不需要先建立一個新啟動的處理序Process執行個體。This overload lets you start a process without first creating a new Process instance. 使用這個多載ProcessStartInfo參數是明確的步驟,建立新的替代Process執行個體,設定其StartInfo內容,然後呼叫StartProcess執行個體。Using 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屬性可以有副檔名為.txt,如果您的文字檔案關聯的編輯器,例如 [記事本],或如果您有使用文字處理工具,例如 Microsoft Word associated.doc 檔案,它可以有副檔名為.doc。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.

您可以藉由指定從中您原先安裝應用程式的位置 (例如,Web 位址) 來啟動 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.UserNameProcessStartInfo.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執行個體,指定的開始資訊 (包括檔案名稱),而且想要啟動處理序資源,並將它與現有關聯Process執行個體。Use that overload when you have already created a Process instance, specified start information (including the file name), and 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. 這個多載並沒有參數的多載可讓您藉由使用指定的處理序資源的啟動資訊ProcessStartInfo執行個體。Both 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:\mypath時啟動它。For 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 Web 網頁和伺服器控制項的程式碼。ASP.NET Web page and server control code executes in the context of the ASP.NET worker process on the Web server. 如果您使用StartASP.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. 關閉使用的處理程序CloseMainWindowKillClose processes using CloseMainWindow or Kill. 您可以檢查處理程序是否已經使用關閉其HasExited屬性。You can check whether a process has already been closed by using its HasExited property.

這裡的附註中的 managed 執行緒的 apartment 狀態是必要的。A note about apartment states in managed threads is necessary here. UseShellExecutetruestartInfo參數,請確定您已在您的應用程式上設定執行緒模型,藉由將屬性[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. Managed 的執行緒在可以的否則為unknown狀態,或放入MTA狀態,後者的 je v konfliktuUseShellExecute正在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. 某些方法需要的 apartment 狀態不是unknownSome methods require that the apartment state not be unknown. 如果狀態未明確設定,當應用程式遇到這種方法,則預設為MTA,而且設定之後,apartment 狀態無法變更。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造成作業系統殼層來管理執行緒時擲回例外狀況。However, MTA causes an exception to be thrown when the operating system shell is managing the thread.

安全性

LinkDemand
針對完全信任立即呼叫端。for full trust for the immediate caller. 這個成員無法供部分信任的程式碼使用。This member cannot be used by partially trusted code.

另請參閱

Start(String) Start(String) Start(String) 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 String String String

要在處理序中執行之文件或應用程式檔案的名稱。The name of a document or application file to run in the 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.

處理序物件已經處置。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()
{
    // 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 = 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 'OpenApplication

        ' Opens urls and .html documents using Internet Explorer.
        Sub 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")
        End Sub 'OpenWithArguments

        ' 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 'OpenWithStartInfo

        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 'Main
    End Class 'MyProcess
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.

這個多載可讓您不需要先建立一個新啟動的處理序Process執行個體。This overload lets you start a process without first creating a new Process instance. 多載是明確的步驟,建立新的替代Process執行個體,設定FileName隸屬StartInfo屬性,然後呼叫StartProcess執行個體。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參數,以在原本安裝的應用程式的位置 (例如,Web 位址)。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.

藉由指定其檔案名稱啟動處理序是類似於輸入中的資訊Run對話方塊中的 WindowsStart功能表。Starting 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. 比方說如果文字檔聯編輯器,例如 [記事本],或如果您有使用文字處理工具,例如 Microsoft Word associated.doc 檔案,它可以有.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 是選擇性的fileName參數。Similarly, 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"或"Notepad"的參數。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執行個體,指定的開始資訊 (包括檔案名稱),而且想要啟動處理序資源,並將它與現有關聯Process執行個體。Use that overload when you have already created a Process instance, specified start information (including the file name), and 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:\mypath時啟動它。For 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 Web 網頁和伺服器控制項的程式碼。ASP.NET Web page and server control code executes in the context of the ASP.NET worker process on the Web server. 如果您使用StartASP.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. 關閉使用的處理程序CloseMainWindowKillClose processes using CloseMainWindow or Kill. 您可以檢查處理程序是否已經使用關閉其HasExited屬性。You can check whether a process has already been closed by using its HasExited property.

這裡的附註中的 managed 執行緒的 apartment 狀態是必要的。A note about apartment states in managed threads is necessary here. UseShellExecutetrue處理序元件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. Managed 的執行緒在可以的否則為unknown狀態,或放入MTA狀態,後者的 je v konfliktuUseShellExecute正在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. 某些方法需要的 apartment 狀態不是unknownSome methods require that the apartment state not be unknown. 如果狀態未明確設定,當應用程式遇到這種方法,則預設為MTA,而且設定之後,apartment 狀態無法變更。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造成作業系統殼層來管理執行緒時擲回例外狀況。However, MTA causes an exception to be thrown when the operating system shell is managing the thread.

安全性

LinkDemand
針對完全信任立即呼叫端。for full trust for the immediate caller. 這個成員無法供部分信任的程式碼使用。This member cannot be used by partially trusted code.

另請參閱

Start(String, String) Start(String, String) Start(String, String) 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 String String String

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

arguments
String String String String

啟動處理程序時要傳遞的命令列引數。Command-line arguments to pass when starting the 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- 引數長度和處理序完整路徑長度的總和超過 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()
{
    // 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 = 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 'OpenApplication

        ' Opens urls and .html documents using Internet Explorer.
        Sub 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")
        End Sub 'OpenWithArguments

        ' 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 'OpenWithStartInfo

        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 'Main
    End Class 'MyProcess
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.

這個多載可讓您不需要先建立一個新啟動的處理序Process執行個體。This overload lets you start a process without first creating a new Process instance. 多載是明確的步驟,建立新的替代Process執行個體,設定FileNameArguments的成員StartInfo屬性,以及呼叫StartProcess執行個體。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.

藉由指定其檔案名稱和引數啟動程序是類似於輸入的檔案名稱和中的命令列引數Run對話方塊中的 WindowsStart功能表。Starting 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. 比方說如果文字檔聯編輯器,例如 [記事本],或如果您有使用文字處理工具,例如 Microsoft Word associated.doc 檔案,它可以有.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 是選擇性的fileName參數。Similarly, 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"或"Notepad"的參數。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執行個體,指定的開始資訊 (包括檔案名稱),而且想要啟動處理序資源,並將它與現有關聯Process執行個體。Use that overload when you have already created a Process instance, specified start information (including the file name), and 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:\mypath時啟動它。For 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 Web 網頁和伺服器控制項的程式碼。ASP.NET Web page and server control code executes in the context of the ASP.NET worker process on the Web server. 如果您使用StartASP.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. 關閉使用的處理程序CloseMainWindowKillClose processes using CloseMainWindow or Kill. 您可以檢查處理程序是否已經使用關閉其HasExited屬性。You can check whether a process has already been closed by using its HasExited property.

這裡的附註中的 managed 執行緒的 apartment 狀態是必要的。A note about apartment states in managed threads is necessary here. UseShellExecutetrue處理序元件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. Managed 的執行緒在可以的否則為unknown狀態,或放入MTA狀態,後者的 je v konfliktuUseShellExecute正在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. 某些方法需要的 apartment 狀態不是unknownSome methods require that the apartment state not be unknown. 如果狀態未明確設定,當應用程式遇到這種方法,則預設為MTA,而且設定之後,apartment 狀態無法變更。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造成作業系統殼層來管理執行緒時擲回例外狀況。However, MTA causes an exception to be thrown when the operating system shell is managing the thread.

安全性

LinkDemand
針對完全信任立即呼叫端。for full trust for the immediate caller. 這個成員無法供部分信任的程式碼使用。This member cannot be used by partially trusted code.

另請參閱

Start(String, String, SecureString, String) Start(String, String, SecureString, String) Start(String, String, SecureString, String) 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)]
public static System.Diagnostics.Process Start (string fileName, string userName, System.Security.SecureString password, string domain);
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 String String String

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

userName
String String String String

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

password
SecureString SecureString SecureString SecureString

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

domain
String String String String

要在啟動處理序時使用的網域。The domain to use when starting the 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.

例外狀況

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

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

此方法在 Linux 或 macOS 上不受支援 (僅限 .NET Core)。Method 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.

這個多載可讓您不需要先建立一個新啟動的處理序Process執行個體。This overload lets you start a process without first creating a new Process instance. 多載是明確的步驟,建立新的替代Process執行個體,設定FileNameUserNamePassword,並Domain屬性StartInfo屬性,並呼叫StartProcess執行個體。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 是選擇性的fileName參數。Similarly, 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"或"Notepad"的參數。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,和domain參數。The 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. 關閉使用的處理程序CloseMainWindowKillClose processes using CloseMainWindow or Kill. 您可以檢查處理程序是否已經使用關閉其HasExited屬性。You can check whether a process has already been closed by using its HasExited property.

安全性

LinkDemand
針對完全信任立即呼叫端。for full trust for the immediate caller. 這個成員無法供部分信任的程式碼使用。This member cannot be used by partially trusted code.

Start(String, String, String, SecureString, String) Start(String, String, String, SecureString, String) Start(String, String, String, SecureString, String) 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)]
public static System.Diagnostics.Process Start (string fileName, string arguments, string userName, System.Security.SecureString password, string domain);
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 String String String

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

arguments
String String String String

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

userName
String String String String

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

password
SecureString SecureString SecureString SecureString

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

domain
String String String String

要在啟動處理序時使用的網域。The domain to use when starting the 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- 引數長度和關聯檔案完整路徑長度的總和超過 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)。Method 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.

這個多載可讓您不需要先建立一個新啟動的處理序Process執行個體。This overload lets you start a process without first creating a new Process instance. 多載是明確的步驟,建立新的替代Process執行個體,設定FileNameArgumentsUserNamePassword,和Domain屬性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 是選擇性的fileName參數。Similarly, 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"或"Notepad"的參數。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,和domain參數。The 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. 關閉使用的處理程序CloseMainWindowKillClose processes using CloseMainWindow or Kill. 您可以檢查處理程序是否已經使用關閉其HasExited屬性。You can check whether a process has already been closed by using its HasExited property.

安全性

LinkDemand
針對完全信任立即呼叫端。for full trust for the immediate caller. 這個成員無法供部分信任的程式碼使用。This member cannot be used by partially trusted code.

適用於