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

通过指定文档或应用程序文件的名称来启动进程资源,并将资源与新的 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, 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);
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

要在进程中运行的应用程序文件的名称。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

包含启动进程时要使用的密码的 SecureStringA SecureString that contains the password to use when starting the process.

domain
String

启动进程时要使用的域。The domain to use when starting the process.

返回

Process

与进程资源关联的新 Process,如果未启动进程资源,则为 nullA 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 属性已设置为 true 的非 null 进程。In 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,则该进程将不会启动,并且将返回 nullIf 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 实例、设置 Password属性的 FileNameArgumentsUserNameDomainStartInfo 属性,以及调用 Start 实例的 ProcessThe 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.txt中的文本文件。If 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.

备注

文件名必须在具有 userNamepassworddomain 参数的 Start 重载中表示可执行文件。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. 使用 CloseMainWindowKill关闭进程。Close processes using CloseMainWindow or Kill. 您可以使用其 HasExited 属性检查进程是否已关闭。You 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)]
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);
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

包含启动进程时要使用的密码的 SecureStringA SecureString that contains the password to use when starting the process.

domain
String

启动进程时要使用的域。The domain to use when starting the process.

返回

Process

与进程资源关联的新 Process,如果未启动进程资源,则为 nullA 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 属性已设置为 true 的非 null 进程。In 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,则该进程将不会启动,并且将返回 nullIf 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 实例、设置 Domain 属性的 FileNameUserNamePasswordStartInfo 属性,以及调用 Start 实例的 ProcessThe 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.txt中的文本文件。If 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.

备注

文件名必须在具有 userNamepassworddomain 参数的 Start 重载中表示可执行文件。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. 使用 CloseMainWindowKill关闭进程。Close processes using CloseMainWindow or Kill. 您可以使用其 HasExited 属性检查进程是否已关闭。You 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,如果未启动进程资源,则为 nullA 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 属性已设置为 true 的非 null 进程。In 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,则该进程将不会启动,并且将返回 nullIf 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 实例、设置 StartInfo 属性的 FileNameArguments 成员以及调用 Start 实例的 ProcessThe 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 Start 菜单的 "Run" 对话框中键入文件名和命令行参数。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. 例如,如果您已将文本文件与一个编辑器(如记事本)关联,则文件名可以具有 .txt 扩展名; 如果您已将 .doc 文件与 word 处理工具(如 Microsoft Word)相关联,则它可能具有 .doc。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. 同样,在 fileName 参数中,.exe 扩展名是可选的,这与 Run 对话框可以接受带有或不带 .exe 扩展名的可执行文件名的方式相同。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.txt中的文本文件。If 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 and specified start information (including the file name), and you want to start a process resource and associate it with the existing Process instance. 若要创建新的 Process 组件,而不是为现有组件启动进程,请使用 static 重载之一。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 工作进程上下文中执行。ASP.NET Web page and server control code executes in the context of the ASP.NET worker process on the Web server. 如果在 ASP.NET 网页或服务器控件中使用 Start 方法,则新进程将在具有受限权限的 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. 使用 CloseMainWindowKill关闭进程。Close processes using CloseMainWindow or Kill. 您可以使用其 HasExited 属性检查进程是否已关闭。You 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. 当在进程组件的 StartInfo 属性中 true UseShellExecute 时,请确保已在应用程序上设置了线程模型,方法是在 main() 方法上设置属性 [STAThread]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 状态,后者与 trueUseShellExecute 发生冲突。Otherwise, 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-

UseShellExecuteStartInfotrue 的值为 RedirectStandardInput时, RedirectStandardOutput属性的 RedirectStandardError 成员的值为 trueThe 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.

不支持 shell 的操作系统(如,仅适用于.NET Core 的 Nano Server)不支持此方法。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

注解

使用此重载来启动进程资源,并将其与当前 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. 如果 StartInfo 属性的 FileName 成员指定的进程资源已在计算机上运行,则不会启动其他进程资源。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. 相反,将重用正在运行的进程资源并返回 falseInstead, 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之前,必须先为此 Process 实例指定 StartInfo 属性信息,因为该信息用于确定要启动的进程资源。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. 相反,你可以为 Process 类本身调用 Start,并在进程启动时创建新的 Process 组件。Instead, you can call Start for the Process class itself, and a new Process component is created if the process was started. 或者,如果重复使用进程,则返回 nullOr, null is returned if a process was reused. 进程资源与 Start 方法返回的新 Process 组件自动关联。The process resource is automatically associated with the new Process component that is returned by the Start method.

StartInfo 成员可用于复制 Windows Start 菜单的 "Run" 对话框的功能。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 扩展名; 如果您已将 .doc 文件与 word 处理工具(如 Microsoft Word)关联,则它可能具有 .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. 使用 StartInfo 属性的 Verb 成员指定这些操作。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. 例如,如果将浏览器指定为 FileName,则可以将 URL 作为参数传递。For 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 工作进程上下文中执行。ASP.NET Web page and server control code executes in the context of the ASP.NET worker process on the Web server. 如果在 ASP.NET 网页或服务器控件中使用 Start 方法,则新进程将在具有受限权限的 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. 使用 CloseMainWindowKill关闭进程。Close processes using CloseMainWindow or Kill. 您可以使用其 HasExited 属性检查进程是否已关闭。You 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. 当在进程组件的 StartInfo 属性中 true UseShellExecute 时,请确保已在应用程序上设置了线程模型,方法是在 main() 方法上设置属性 [STAThread]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 状态,后者与 trueUseShellExecute 发生冲突。Otherwise, 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);
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,如果未启动进程资源,则为 nullA 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 属性已设置为 true 的非 null 进程。In 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."

不支持 shell 的操作系统(如,仅适用于.NET Core 的 Nano Server)不支持此方法。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()
{
    // URLs are not considered documents. They can only be opened
    // by passing them as arguments.
    Process::Start("IExplore.exe", "www.northwindtraders.com");

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

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

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

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

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

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

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

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

            Process.Start(startInfo);

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

            Process.Start(startInfo);
        }

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

            MyProcess myProcess = new MyProcess();

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

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

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

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

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

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

            Process.Start(startInfo)

            startInfo.Arguments = "www.northwindtraders.com"

            Process.Start(startInfo)
        End Sub

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

            Dim myProcess As New MyProcess()

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

注解

使用此重载通过指定 ProcessStartInfo 实例来启动进程资源。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,则该进程将不会启动,并且将返回 nullIf 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 属性和调用 Process 实例的 Start 的显式步骤的替代方法。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 扩展名; 如果您已将 .doc 文件与 word 处理工具(如 Microsoft Word)关联,则它可能具有 .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.

如果设置了 StartInfo 实例的 ProcessStartInfo.UserNameProcessStartInfo.Password 属性,则会调用非托管 CreateProcessWithLogonW 函数,这将在新窗口中启动该进程,即使 ProcessStartInfo.CreateNoWindow 属性值 trueProcessStartInfo.WindowStyle 属性值 ProcessWindowStyle.Hidden也是如此。If 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. 如果 nullProcessStartInfo.Domain 属性,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 and specified start information (including the file name), and you want to start a process resource and associate it with the existing Process instance. 若要创建新的 Process 组件,而不是为现有组件启动进程,请使用 static 重载之一。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 工作进程上下文中执行。ASP.NET Web page and server control code executes in the context of the ASP.NET worker process on the Web server. 如果在 ASP.NET 网页或服务器控件中使用 Start 方法,则新进程将在具有受限权限的 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. 使用 CloseMainWindowKill关闭进程。Close processes using CloseMainWindow or Kill. 您可以使用其 HasExited 属性检查进程是否已关闭。You 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. true startInfo 参数上 UseShellExecute 时,请确保已在应用程序上设置了线程模型,方法是在 main() 方法上设置属性 [STAThread]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 状态,后者与 trueUseShellExecute 发生冲突。Otherwise, 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,如果未启动进程资源,则为 nullA 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 属性已设置为 true 的非 null 进程。In 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,则该进程将不会启动,并且将返回 nullIf 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 实例,设置 StartInfo 属性的 FileName 成员,并为 Process 实例调用 StartThe 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.

可以通过将 fileName 参数设置为最初安装应用程序的位置(例如,Web 地址)来启动 ClickOnce 应用程序。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 Start 菜单的 "Run" 对话框中键入信息。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. 例如,如果您已将文本文件与一个编辑器(如记事本)关联,则文件名可以具有 .txt 扩展名; 如果您已将 .doc 文件与 word 处理工具(如 Microsoft Word)相关联,则它可能具有 .doc。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. 同样,在 fileName 参数中,.exe 扩展名是可选的,这与 Run 对话框可以接受带有或不带 .exe 扩展名的可执行文件名的方式相同。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 and specified start information (including the file name), and you want to start a process resource and associate it with the existing Process instance. 若要创建新的 Process 组件,而不是为现有组件启动进程,请使用 static 重载之一。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 工作进程上下文中执行。ASP.NET Web page and server control code executes in the context of the ASP.NET worker process on the Web server. 如果在 ASP.NET 网页或服务器控件中使用 Start 方法,则新进程将在具有受限权限的 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. 使用 CloseMainWindowKill关闭进程。Close processes using CloseMainWindow or Kill. 您可以使用其 HasExited 属性检查进程是否已关闭。You 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. 当在进程组件的 StartInfo 属性中 true UseShellExecute 时,请确保已在应用程序上设置了线程模型,方法是在 main() 方法上设置属性 [STAThread]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 状态,后者与 trueUseShellExecute 发生冲突。Otherwise, 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.

另请参阅

适用于