Process.Start 方法

定义

启动进程资源并将其与 Process 组件关联。

重载

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

通过指定应用程序的名称、一组命令行自变量、用户名、密码和域来启动一个进程资源,并将该资源与新的 Process 组件关联起来。

Start(String, String, SecureString, String)

通过指定应用程序的名称、用户名、密码和域来启动一个进程资源,并将该资源与新的 Process 组件关联起来。

Start(String, String)

通过指定应用程序的名称和一组命令行参数来启动一个进程资源,并将该资源与新的 Process 组件相关联。

Start(String)

通过指定文档或应用程序文件的名称来启动进程资源,并将资源与新的 Process 组件关联。

Start(ProcessStartInfo)

启动由包含进程启动信息(例如,要启动的进程的文件名)的参数指定的进程资源,并将该资源与新的 Process 组件关联。

Start()

启动(或重用)此 Process 组件的 StartInfo 属性指定的进程资源,并将其与该组件关联。

Start(String, IEnumerable<String>)

通过指定应用程序的名称和一组命令行参数来启动一个进程资源。

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

重要

此 API 不符合 CLS。

通过指定应用程序的名称、一组命令行自变量、用户名、密码和域来启动一个进程资源,并将该资源与新的 Process 组件关联起来。

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

参数

fileName
String

要在进程中运行的应用程序文件的名称。

arguments
String

启动该进程时传递的命令行参数。

userName
String

启动进程时使用的用户名。

password
SecureString

包含启动进程时要使用的密码的 SecureString

domain
String

启动进程时要使用的域。

返回

与进程资源关联的新 Process,如果未启动进程资源,则为 null。 请注意,伴随同一进程中已运行的实例而启动的新进程将独立于其他进程。 此外,启动可能返回一个 HasExited 属性已设置为 true 的非 null 进程。 在这种情况下,启动的进程可能已激活现有实例自身,然后退出。

属性

例外

未指定文件名。

打开关联的文件时出错。

- 或 -

找不到 fileName 中指定的文件。

- 或 -

参数的长度与该关联文件的完整路径的长度的总和超过了 2080。 与此异常关联的错误消息可以是以下消息之一:“传递给系统调用的数据区域太小”或“拒绝访问”。

已释放此进程对象。

Linux 或 macOS(仅适用于.NET Core)不支持此成员。

注解

使用此重载通过指定其文件名、命令行参数、用户名、密码和域来创建新进程及其主线程。 然后,新进程在指定凭据的安全上下文中运行指定的可执行文件, (用户、域和密码) 。

注意

当可执行文件位于远程驱动器上时,必须使用统一的资源标识符 (URI) (而不是链接驱动器号)来标识网络共享。

注意

如果要启动的可执行文件的地址为 URL,则不会启动进程,并且 null 返回该进程。

此重载使你无需先创建新 Process 实例即可启动进程。 重载是创建一个新Process实例、设置属性的 FileNameArgumentsUserNamePassword、 和 Domain 属性StartInfo以及调用 Start 实例的显式步骤的Process替代方法。

同样,与 “运行 ” 对话框可以接受包含或不使用 .exe 扩展名的可执行文件名称一样,.exe扩展在 参数中 fileName 是可选的。 例如,可以将 参数设置为 fileName “Notepad.exe”或“记事本”。 fileName如果 参数表示可执行文件,则 arguments 参数可能表示要处理的文件,例如 中的Notepad.exe myfile.txt文本文件。

注意

文件名必须表示重载中 Start 具有 userNamepassworddomain 参数的可执行文件。

每当使用 Start 启动进程时,都可能需要关闭它,否则可能会丢失系统资源。 使用 CloseMainWindowKill关闭进程。 可以使用进程 HasExited 属性检查进程是否已关闭。

适用于

Start(String, String, SecureString, String)

重要

此 API 不符合 CLS。

通过指定应用程序的名称、用户名、密码和域来启动一个进程资源,并将该资源与新的 Process 组件关联起来。

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

参数

fileName
String

要在进程中运行的应用程序文件的名称。

userName
String

启动进程时使用的用户名。

password
SecureString

包含启动进程时要使用的密码的 SecureString

domain
String

启动进程时要使用的域。

返回

与进程资源关联的新 Process,如果未启动进程资源,则为 null。 请注意,伴随同一进程中已运行的实例而启动的新进程将独立于其他进程。 此外,启动可能返回一个 HasExited 属性已设置为 true 的非 null 进程。 在这种情况下,启动的进程可能已激活现有实例自身,然后退出。

属性

例外

未指定文件名。

打开关联的文件时出错。

- 或 -

找不到 fileName 中指定的文件。

已释放此进程对象。

Linux 或 macOS(仅适用于.NET Core)不支持此成员。

示例

下面的代码示例演示了如何使用此重载启动可执行文件,还演示了在尝试启动与不可执行文件关联的应用程序时引发 Win32Exception 的 。

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

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

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

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

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

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

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

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

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

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

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

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

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

注解

使用此重载通过指定其文件名、用户名、密码和域来创建新进程及其主线程。 然后,新进程在指定凭据的安全上下文中运行指定的可执行文件, (用户、域和密码) 。

注意

当可执行文件位于远程驱动器上时,必须使用统一的资源标识符 (URI) (而不是链接驱动器号)来标识网络共享。

注意

如果要启动的可执行文件的地址为 URL,则不会启动进程,并且 null 返回该进程。

此重载使你无需先创建新 Process 实例即可启动进程。 重载是创建新 Process 实例、设置属性的 FileNameUserNamePasswordDomain 属性 StartInfo 以及为实例调用 Start 的显式步骤的 Process 替代方法。

同样,与 “运行 ” 对话框可以接受包含或不使用 .exe 扩展名的可执行文件名称一样,.exe扩展在 参数中 fileName 是可选的。 例如,可以将 参数设置为 fileName “Notepad.exe”或“记事本”。 fileName如果 参数表示可执行文件,则 arguments 参数可能表示要处理的文件,例如 中的Notepad.exe myfile.txt文本文件。

注意

文件名必须表示重载中 Start 具有 userNamepassworddomain 参数的可执行文件。

每当使用 Start 启动进程时,都可能需要关闭它,否则可能会丢失系统资源。 使用 CloseMainWindowKill关闭进程。 可以使用进程 HasExited 属性检查进程是否已关闭。

适用于

Start(String, String)

通过指定应用程序的名称和一组命令行参数来启动一个进程资源,并将该资源与新的 Process 组件相关联。

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

参数

fileName
String

要在进程中运行的应用程序文件的名称。

arguments
String

启动该进程时传递的命令行参数。

返回

与进程资源关联的新 Process,如果未启动进程资源,则为 null。 请注意,伴随同一进程中已运行的实例而启动的新进程将独立于其他进程。 此外,启动可能返回一个 HasExited 属性已设置为 true 的非 null 进程。 在这种情况下,启动的进程可能已激活现有实例自身,然后退出。

属性

例外

fileNamearguments 参数为 null

打开关联的文件时出错。

- 或 -

找不到 fileName 中指定的文件。

- 或 -

参数的长度与该进程的完整路径的长度的总和超过了 2080。 与此异常关联的错误消息可以是以下消息之一:“传递给系统调用的数据区域太小”或“拒绝访问”。

已释放此进程对象。

PATH 环境变量具有一个包含引号的字符串。

示例

以下示例首先生成 Internet Explorer 实例,并在浏览器中显示收藏夹文件夹的内容。 然后,它启动 Internet Explorer 的其他一些实例,并显示一些特定的页面或站点。 最后,它会启动 Internet Explorer,在导航到特定站点时窗口最小化。

#using <System.dll>

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

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

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

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

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

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

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

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

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

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

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

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

            Process.Start(startInfo);

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

            Process.Start(startInfo);
        }

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

            MyProcess myProcess = new MyProcess();

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

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

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

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

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

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

            Process.Start(startInfo)

            startInfo.Arguments = "www.northwindtraders.com"

            Process.Start(startInfo)
        End Sub

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

            Dim myProcess As New MyProcess()

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

注解

使用此重载通过指定进程资源的文件名和命令行参数来启动该资源。 重载将资源与新 Process 对象相关联。

注意

如果要启动的可执行文件的地址为 URL,则不会启动进程,并且 null 返回该进程。

此重载使你无需先创建新 Process 实例即可启动进程。 重载是创建新Process实例、设置 FileName 属性的 StartInfoArguments 成员以及为实例调用Start的显式步骤的Process替代方法。

通过指定其文件名和参数启动进程类似于在 Run Windows Start 菜单的对话框中键入文件名和命令行参数。 因此,文件名不需要表示可执行文件。 它可以是扩展已与系统上安装的应用程序关联的任何文件类型。 例如,如果具有与编辑器(如记事本)关联的文本文件,则文件名可以具有.txt扩展名;如果已将.doc文件与字处理工具(如 Microsoft Word)关联,则文件名可以具有.doc。 同样,与对话框可以接受带或不带.exe扩展名的可执行文件名称一样 Run ,.exe扩展名在 参数中 fileName 是可选的。 例如,可以将 参数设置为 fileName “Notepad.exe”或“记事本”。 fileName如果 参数表示可执行文件,则 arguments 参数可能表示要处理的文件,例如 中的Notepad.exe myfile.txt文本文件。 fileName如果 参数表示 (.cmd) 文件的命令,则arguments参数必须包含“”/c或“”/k参数,以指定命令窗口是在完成后退出还是保留。

与其他重载不同,没有参数的 的 Start 重载不是 成员 static 。 如果已创建 Process 实例并指定启动信息 (包括文件名) ,并且想要启动进程资源并将其与现有 Process 实例相关联,请使用该重载。 static如果要创建新Process组件而不是为现有组件启动进程,请使用其中一个重载。 此重载和没有参数的重载都允许指定要启动的进程资源的文件名和要传递的命令行参数。

如果在系统中使用引号声明了路径变量,则必须在启动在该位置找到的任何进程时完全限定该路径。 否则,系统将找不到路径。 例如,如果 c:\mypath 路径中没有 ,并且使用引号添加它: path = %path%;"c:\mypath",则必须在启动时完全限定中的任何 c:\mypath 进程。

注意

ASP.NET 网页和服务器控制代码在 Web 服务器上 ASP.NET 工作进程的上下文中执行。 如果在 ASP.NET 网页或服务器控件中使用 Start 方法,则新进程将在具有受限权限的 Web 服务器上执行。 进程不会在与客户端浏览器相同的上下文中启动,并且无权访问用户桌面。

每当使用 Start 启动进程时,都可能需要关闭它,否则可能会丢失系统资源。 使用 CloseMainWindowKill关闭进程。 可以使用进程 HasExited 属性检查进程是否已关闭。

此处需要提供有关托管线程中的单元状态的说明。 当 进程组件的 StartInfo 属性上为 trueUseShellExecute,请确保通过在 方法上设置 属性[STAThread],在应用程序上main()设置线程模型。 否则,托管线程可能处于 状态 unknown 或置于 MTA 状态,后者与 UseShellExecutetrue冲突。 某些方法要求单元状态不是 unknown。 如果未显式设置状态,当应用程序遇到此类方法时,它默认为 MTA,并且一旦设置,单元状态便无法更改。 但是, MTA 会导致在操作系统 shell 管理线程时引发异常。

另请参阅

适用于

Start(String)

通过指定文档或应用程序文件的名称来启动进程资源,并将资源与新的 Process 组件关联。

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

参数

fileName
String

要在进程中运行的文档或应用程序文件的名称。

返回

与进程资源关联的新 Process,如果未启动进程资源,则为 null。 请注意,伴随同一进程中已运行的实例而启动的新进程将独立于其他进程。 此外,启动可能返回一个 HasExited 属性已设置为 true 的非 null 进程。 在这种情况下,启动的进程可能已激活现有实例自身,然后退出。

属性

例外

打开关联的文件时出错。

- 或 -

找不到 fileName 中指定的文件。

已释放此进程对象。

PATH 环境变量具有一个包含引号的字符串。

示例

以下示例首先生成 Internet Explorer 实例,并在浏览器中显示收藏夹文件夹的内容。 然后,它启动 Internet Explorer 的其他一些实例,并显示一些特定的页面或站点。 最后,它会启动 Internet Explorer,在导航到特定站点时窗口最小化。

#using <System.dll>

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

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

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

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

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

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

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

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

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

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

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

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

            Process.Start(startInfo);

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

            Process.Start(startInfo);
        }

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

            MyProcess myProcess = new MyProcess();

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

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

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

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

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

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

            Process.Start(startInfo)

            startInfo.Arguments = "www.northwindtraders.com"

            Process.Start(startInfo)
        End Sub

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

            Dim myProcess As New MyProcess()

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

注解

使用此重载通过指定其文件名来启动进程资源。 重载将资源与新 Process 对象相关联。

注意

如果要启动的可执行文件的地址为 URL,则不会启动进程,并且 null 返回该进程。

此重载使你无需先创建新 Process 实例即可启动进程。 重载是创建新 Process 实例、设置 FileName 属性的成员 StartInfo 以及为实例调用 Start 的显式步骤的 Process 替代方法。

可以通过将 参数设置为 fileName 位置来启动 ClickOnce 应用程序 (例如,最初安装应用程序的 Web 地址) 。 不要通过指定 ClickOnce 应用程序在硬盘驱动器上的安装位置来启动该应用程序。

通过指定进程文件名启动进程类似于在 Run Windows Start 菜单的对话框中键入信息。 因此,文件名不需要表示可执行文件。 它可以是扩展已与系统上安装的应用程序关联的任何文件类型。 例如,如果具有与编辑器(如记事本)关联的文本文件,则文件名可以具有.txt扩展名;如果已将.doc文件与字处理工具(如 Microsoft Word)关联,则文件名可以具有.doc。 同样,与对话框可以接受带或不带.exe扩展名的可执行文件名称一样 Run ,.exe扩展名在 参数中 fileName 是可选的。 例如,可以将 参数设置为 fileName “Notepad.exe”或“记事本”。

此重载不允许为进程使用命令行参数。 如果需要为进程指定一个或多个命令行参数,请使用 Process.Start(ProcessStartInfo)Process.Start(String, String) 重载。

与其他重载不同,没有参数的 的 Start 重载不是 成员 static 。 如果已创建 Process 实例并指定启动信息 (包括文件名) ,并且想要启动进程资源并将其与现有 Process 实例相关联,请使用该重载。 static如果要创建新Process组件而不是为现有组件启动进程,请使用其中一个重载。 此重载和没有参数的重载都允许指定要启动的进程资源的文件名。

如果在系统中使用引号声明了路径变量,则必须在启动在该位置找到的任何进程时完全限定该路径。 否则,系统将找不到路径。 例如,如果 c:\mypath 路径中没有 ,并且使用引号添加它: path = %path%;"c:\mypath",则必须在启动时完全限定中的任何 c:\mypath 进程。

注意

ASP.NET 网页和服务器控制代码在 Web 服务器上 ASP.NET 工作进程的上下文中执行。 如果在 ASP.NET 网页或服务器控件中使用 Start 方法,则新进程将在具有受限权限的 Web 服务器上执行。 进程不会在与客户端浏览器相同的上下文中启动,并且无权访问用户桌面。

每当使用 Start 启动进程时,都可能需要关闭它,否则可能会丢失系统资源。 使用 CloseMainWindowKill关闭进程。 可以使用进程 HasExited 属性检查进程是否已关闭。

此处需要提供有关托管线程中的单元状态的说明。 当 进程组件的 StartInfo 属性上为 trueUseShellExecute,请确保通过在 方法上设置 属性[STAThread],在应用程序上main()设置线程模型。 否则,托管线程可能处于 状态 unknown 或置于 MTA 状态,后者与 UseShellExecutetrue冲突。 某些方法要求单元状态不是 unknown。 如果未显式设置状态,当应用程序遇到此类方法时,它默认为 MTA,并且一旦设置,单元状态便无法更改。 但是, MTA 会导致在操作系统 shell 管理线程时引发异常。

另请参阅

适用于

Start(ProcessStartInfo)

启动由包含进程启动信息(例如,要启动的进程的文件名)的参数指定的进程资源,并将该资源与新的 Process 组件关联。

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

参数

startInfo
ProcessStartInfo

ProcessStartInfo,包含用于启动进程的信息(包括文件名和任何命令行参数)。

返回

与进程资源关联的新 Process,如果未启动进程资源,则为 null。 请注意,伴随同一进程中已运行的实例而启动的新进程将独立于其他进程。 此外,启动可能返回一个 HasExited 属性已设置为 true 的非 null 进程。 在这种情况下,启动的进程可能已激活现有实例自身,然后退出。

属性

例外

startInfo 参数的 FileName 属性中未指定任何文件名。

- 或 -

startInfo 参数的 UseShellExecute 属性值为 true,且 RedirectStandardInputRedirectStandardOutputRedirectStandardError 属性的值也为 true

- 或 -

startInfo 参数的 UseShellExecute 属性值为 true,且 UserName 属性值不为 null 或空,或者 Password 属性值不为 null

startInfo 参数为 null

已释放此进程对象。

打开关联的文件时出错。

- 或 -

startInfo 参数的 FileName 属性中指定的文件未找到。

- 或 -

参数的长度与该进程的完整路径的长度的总和超过了 2080。 与此异常关联的错误消息可以是以下消息之一:“传递给系统调用的数据区域太小”或“拒绝访问”。

不支持 shell 的操作系统(如,仅适用于.NET Core 的 Nano Server)不支持此方法。

示例

以下示例首先生成 Internet Explorer 实例,并在浏览器中显示收藏夹文件夹的内容。 然后,它启动 Internet Explorer 的其他一些实例,并显示一些特定的页面或站点。 最后,它会启动 Internet Explorer,在导航到特定站点时窗口最小化。

有关此方法的其他用法的其他示例,请参阅 类的各个 ProcessStartInfo 属性。

#using <System.dll>

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

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

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

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

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

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

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

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

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

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

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

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

            Process.Start(startInfo);

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

            Process.Start(startInfo);
        }

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

            MyProcess myProcess = new MyProcess();

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

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

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

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

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

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

            Process.Start(startInfo)

            startInfo.Arguments = "www.northwindtraders.com"

            Process.Start(startInfo)
        End Sub

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

            Dim myProcess As New MyProcess()

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

注解

使用此重载通过指定 ProcessStartInfo 实例来启动进程资源。 重载将资源与新 Process 对象相关联。

注意

如果要启动的可执行文件的地址为 URL,则不会启动进程,并且 null 返回该进程。

此重载使你无需先创建新 Process 实例即可启动进程。 将此重载与参数一 ProcessStartInfo 起使用是创建新 Process 实例、设置其 StartInfo 属性和调用 Start 实例的显式步骤的 Process 替代方法。

ProcessStartInfo使用 实例作为 参数,可以Start让你对传入调用的内容进行调用,以最能控制该调用以启动进程。 如果只需要传递文件名或文件名和参数,则无需创建新 ProcessStartInfo 实例,尽管这是一个选项。 唯 Process.StartInfo 一必须设置的属性是 FileName 属性。 属性 FileName 不需要表示可执行文件。 它可以是扩展已与系统上安装的应用程序关联的任何文件类型。 例如,FileName如果具有与编辑器(如记事本)关联的文本文件,则 属性可以具有.txt扩展名;如果已将.doc文件与字处理工具(如 Microsoft Word)相关联,则属性可以具有.doc扩展名。

可以通过指定位置 (启动 ClickOnce 应用程序,例如,最初安装应用程序的 Web 地址) 。 不要通过指定 ClickOnce 应用程序在硬盘驱动器上的安装位置来启动该应用程序。

ProcessStartInfo.UserName如果设置了 实例的 StartInfoProcessStartInfo.Password 属性,则调用非托管CreateProcessWithLogonW函数,该函数在新窗口中启动进程,即使ProcessStartInfo.CreateNoWindow属性值为 trueProcessStartInfo.WindowStyle 属性值为 ProcessWindowStyle.HiddenProcessStartInfo.Domain如果该属性为 null,则ProcessStartInfo.UserName属性必须采用 UPN 格式,用户@DNS_domain_name

与其他重载不同,没有参数的 的 Start 重载不是 成员 static 。 如果已创建 Process 实例并指定启动信息 (包括文件名) ,并且想要启动进程资源并将其与现有 Process 实例相关联,请使用该重载。 static如果要创建新Process组件而不是为现有组件启动进程,请使用其中一个重载。 此重载和没有参数的重载都允许使用 ProcessStartInfo 实例指定进程资源的启动信息。

如果在系统中使用引号声明了路径变量,则必须在启动在该位置找到的任何进程时完全限定该路径。 否则,系统将找不到路径。 例如,如果 c:\mypath 路径中没有 ,并且使用引号添加它: path = %path%;"c:\mypath",则必须在启动时完全限定中的任何 c:\mypath 进程。

注意

ASP.NET 网页和服务器控制代码在 Web 服务器上 ASP.NET 工作进程的上下文中执行。 如果在 ASP.NET 网页或服务器控件中使用 Start 方法,则新进程将在具有受限权限的 Web 服务器上执行。 进程不会在与客户端浏览器相同的上下文中启动,并且无权访问用户桌面。

每当使用 Start 启动进程时,都可能需要关闭它,否则可能会丢失系统资源。 使用 CloseMainWindowKill关闭进程。 可以使用进程 HasExited 属性检查进程是否已关闭。

此处需要提供有关托管线程中的单元状态的说明。 当 参数上为 startInfotrueUseShellExecute,请确保通过在 方法上设置 属性[STAThread],在应用程序上main()设置线程模型。 否则,托管线程可能处于 状态 unknown 或置于 MTA 状态,后者与 UseShellExecutetrue冲突。 某些方法要求单元状态不是 unknown。 如果未显式设置状态,当应用程序遇到此类方法时,它默认为 MTA,并且一旦设置,单元状态便无法更改。 但是, MTA 会导致在操作系统 shell 管理线程时引发异常。

另请参阅

适用于

Start()

启动(或重用)此 Process 组件的 StartInfo 属性指定的进程资源,并将其与该组件关联。

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

返回

如果启动了进程资源,则为 true;如果没有启动新的进程资源(例如,如果重用了现有进程),则为 false

属性

例外

Process 组件的 StartInfo 中未指定任何文件名。

- 或 -

UseShellExecuteStartInfotrue 的值为 RedirectStandardInput时, RedirectStandardOutput属性的 RedirectStandardError 成员的值为 true

打开关联的文件时出错。

已释放此进程对象。

不支持 shell 的操作系统(如,仅适用于.NET Core 的 Nano Server)不支持此方法。

示例

以下示例使用 类的 Process 实例来启动进程。

#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;

int main()
{
    Process^ myProcess = gcnew Process;

    try
    {
        myProcess->StartInfo->UseShellExecute = false;
        // You can start any process, HelloWorld is a do-nothing example.
        myProcess->StartInfo->FileName = "C:\\HelloWorld.exe";
        myProcess->StartInfo->CreateNoWindow = true;
        myProcess->Start();
        // This code assumes the process you are starting will terminate itself. 
        // Given that it is started without a window so you cannot terminate it 
        // on the desktop, it must terminate itself or you can do it programmatically
        // from this application using the Kill method.
    }
    catch ( Exception^ e ) 
    {
        Console::WriteLine( e->Message );
    }
}
using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
    class MyProcess
    {
        public static void Main()
        {
            try
            {
                using (Process myProcess = new Process())
                {
                    myProcess.StartInfo.UseShellExecute = false;
                    // You can start any process, HelloWorld is a do-nothing example.
                    myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
                    myProcess.StartInfo.CreateNoWindow = true;
                    myProcess.Start();
                    // This code assumes the process you are starting will terminate itself.
                    // Given that it is started without a window so you cannot terminate it
                    // on the desktop, it must terminate itself or you can do it programmatically
                    // from this application using the Kill method.
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}
Imports System.Diagnostics
Imports System.ComponentModel

Namespace MyProcessSample
    Class MyProcess
        Public Shared Sub Main()
            Try
                Using myProcess As New Process()

                    myProcess.StartInfo.UseShellExecute = False
                    ' You can start any process, HelloWorld is a do-nothing example.
                    myProcess.StartInfo.FileName = "C:\\HelloWorld.exe"
                    myProcess.StartInfo.CreateNoWindow = True
                    myProcess.Start()
                    ' This code assumes the process you are starting will terminate itself. 
                    ' Given that it is started without a window so you cannot terminate it 
                    ' on the desktop, it must terminate itself or you can do it programmatically
                    ' from this application using the Kill method.
                End Using
            Catch e As Exception
                Console.WriteLine((e.Message))
            End Try
        End Sub
    End Class
End Namespace

注解

使用此重载启动进程资源并将其与当前 Process 组件相关联。 返回值 true 指示已启动新的进程资源。 如果 由 属性的成员StartInfo指定的FileName进程资源已在计算机上运行,则不会启动其他进程资源。 相反,将重复使用正在运行的进程资源并 false 返回该资源。

可以通过指定位置 (启动 ClickOnce 应用程序,例如,最初安装应用程序的 Web 地址) 。 不要通过指定 ClickOnce 应用程序在硬盘驱动器上的安装位置来启动该应用程序。

注意

如果使用的是 Visual Studio,则此方法的 Start 重载是在将组件拖动 Process 到设计器后插入代码中的重载。 Properties使用 窗口展开类别,StartInfo并将相应的值FileName写入 属性。 所做的更改将显示在窗体的 InitializeComponent 过程中。

Start 此重载不是方法 static 。 必须从 类的 Process 实例调用它。 在调用 Start之前,必须先为此Process实例指定StartInfo属性信息,因为该信息用于确定要启动的进程资源。

方法的其他重载 Startstatic 成员。 在调用 方法的 Process 重载之前,无需创建 组件的实例。 相反,你可以为 Process 类本身调用 Start ,如果进程已启动,则会创建一个新Process组件。 或者, null 如果重新使用某个进程,则返回 。 进程资源自动与 方法返回Start的新Process组件相关联。

成员StartInfo可用于复制 Windows Start 菜单对话框的功能Run。 可以在命令行中键入的任何内容都可以通过在 属性中 StartInfo 设置适当的值来启动。 唯 StartInfo 一必须设置的属性是 FileName 属性。 属性 FileName 不一定是可执行文件。 它可以是扩展已与系统上安装的应用程序关联的任何文件类型。 例如,FileName如果具有与编辑器(如记事本)关联的文本文件,则 属性可以具有.txt扩展名;如果已将.doc文件与字处理工具(如 Microsoft Word)相关联,则属性可以具有.doc扩展名。

在命令行中,可以指定要对某些类型的文件执行的操作。 例如,可以打印文档或编辑文本文件。 使用 Verb 属性的成员 StartInfo 指定这些操作。 对于其他类型的文件,可以在从 Run 对话框启动文件时指定命令行参数。 例如,如果将浏览器指定为 ,则可以将 URL 作为 FileName参数传递。 可以在属性的 Arguments 成员中StartInfo指定这些参数。

如果在系统中使用引号声明了路径变量,则必须在启动在该位置找到的任何进程时完全限定该路径。 否则,系统将找不到路径。 例如,如果 c:\mypath 路径中没有 ,并且使用引号添加它: path = %path%;"c:\mypath",则必须在启动时完全限定中的任何 c:\mypath 进程。

注意

ASP.NET 网页和服务器控制代码在 Web 服务器上 ASP.NET 工作进程的上下文中执行。 如果在 ASP.NET 网页或服务器控件中使用 Start 方法,则新进程将在具有受限权限的 Web 服务器上执行。 进程不会在与客户端浏览器相同的上下文中启动,并且无权访问用户桌面。

每当使用 Start 启动进程时,都可能需要关闭它,否则可能会丢失系统资源。 使用 CloseMainWindowKill关闭进程。 可以使用进程 HasExited 属性检查进程是否已关闭。

此处需要提供有关托管线程中的单元状态的说明。 当 进程组件的 StartInfo 属性上为 trueUseShellExecute,请确保通过在 方法上设置 属性[STAThread],在应用程序上main()设置线程模型。 否则,托管线程可能处于 状态 unknown 或置于 MTA 状态,后者与 UseShellExecutetrue冲突。 某些方法要求单元状态不是 unknown。 如果未显式设置状态,当应用程序遇到此类方法时,它默认为 MTA,并且一旦设置,单元状态便无法更改。 但是, MTA 会导致在操作系统 shell 管理线程时引发异常。

另请参阅

适用于

Start(String, IEnumerable<String>)

通过指定应用程序的名称和一组命令行参数来启动一个进程资源。

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

参数

fileName
String

要在进程中运行的文档或应用程序文件的名称。

arguments
IEnumerable<String>

启动该进程时传递的命令行参数。

返回

与进程资源关联的新 Process,如果未启动进程资源,则为 null

属性

注解

如果需要,将自动转义每个参数。

适用于