通过


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)

Source:
Process.Unix.cs
Source:
Process.Unix.cs
Source:
Process.Unix.cs
Source:
Process.Unix.cs
Source:
Process.Unix.cs

重要

此 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

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

domain
String

启动进程时要使用的域。

返回

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

属性

例外

未指定文件名。

打开关联的文件时出错。

-或-

找不到在该文件中指定的 fileName 文件。

-或-

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

进程对象已释放。

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

注解

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

重要

使用不受信任的数据调用此方法是一种安全风险。 仅使用受信任的数据调用此方法。 有关详细信息,请参阅验证所有输入

注释

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

注释

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

通过此重载,无需先创建新 Process 实例即可启动进程。 重载是创建新Process实例、设置属性、设置PasswordUserNameArgumentsFileName属性和StartInfoDomain调用StartProcess实例的显式步骤的替代方法。

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

注释

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

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

适用于

Start(String, String, SecureString, String)

Source:
Process.Unix.cs
Source:
Process.Unix.cs
Source:
Process.Unix.cs
Source:
Process.Unix.cs
Source:
Process.Unix.cs

重要

此 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

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

domain
String

启动进程时要使用的域。

返回

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

属性

例外

未指定文件名。

打开关联文件时出错。

-或-

找不到在该文件中指定的 fileName 文件。

进程对象已释放。

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

示例

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

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

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

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

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

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

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

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

printf "Enter your domain: "
let domain = stdin.ReadLine()
printf "Enter you user name: "
let uname = stdin.ReadLine()
printf "Enter your password: "
let password = new SecureString()

let mutable key = Console.ReadKey(true)

while key.Key <> ConsoleKey.Enter do
    // Ignore any key out of range.
    if int key.Key >= 33 && int key.Key <= 90 && key.Key <> ConsoleKey.Enter then
        // Append the character to the password.
        password.AppendChar key.KeyChar
        Console.Write "*"

    key <- Console.ReadKey(true)

printfn ""

try
    printfn "\nTrying to launch NotePad using your login information..."
    Process.Start("notepad.exe", uname, password, domain) |> ignore
with :? Win32Exception as ex ->
    printfn $"{ex.Message}"

let path = Environment.GetFolderPath Environment.SpecialFolder.MyDocuments + @"\"

try
    // The following call to Start succeeds if test.txt exists.
    printfn "\nTrying to launch 'text.txt'..."
    Process.Start $"{path}text.txt" |> ignore
with :? Win32Exception as ex ->
    printfn $"{ex.Message}"

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.
        printfn "\nTrying to launch 'text.txt' with your login information..."
        Process.Start($"{path}text.txt", uname, password, domain) |> ignore
    with :? Win32Exception as ex ->
        printfn $"{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实例、设置FileNameUserNamePassword属性和StartInfoDomain属性以及调用StartProcess实例的显式步骤的替代方法。

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

注释

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

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

适用于

Start(String, String)

Source:
Process.cs
Source:
Process.cs
Source:
Process.cs
Source:
Process.cs
Source:
Process.cs

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

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

参数

fileName
String

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

arguments
String

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

返回

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

属性

例外

fileNamearguments参数为 null.

打开关联的文件时出错。

-或-

找不到在该文件中指定的 fileName 文件。

-或-

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

进程对象已释放。

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

示例

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

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();
        }
    }
}
module processstartstatic

open System
open System.Diagnostics

// Opens the Internet Explorer application.
let openApplication (myFavoritesPath: string) =
    // Start Internet Explorer. Defaults to the home page.
    Process.Start "IExplore.exe" |> ignore

    // Display the contents of the favorites folder in the browser.
    Process.Start myFavoritesPath |> ignore

// Opens urls and .html documents using Internet Explorer.
let openWithArguments () =
    // url's are not considered documents. They can only be opened
    // by passing them as arguments.
    Process.Start("IExplore.exe", "www.northwindtraders.com") |> ignore

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

// Uses the ProcessStartInfo class to start new processes,
// both in a minimized mode.
let openWithStartInfo () =
    let startInfo = ProcessStartInfo "IExplore.exe"
    startInfo.WindowStyle <- ProcessWindowStyle.Minimized

    Process.Start startInfo |> ignore

    startInfo.Arguments <- "www.northwindtraders.com"

    Process.Start startInfo |> ignore

// Get the path that stores favorite links.
let myFavoritesPath = Environment.GetFolderPath Environment.SpecialFolder.Favorites

openApplication myFavoritesPath
openWithArguments ()
openWithStartInfo ()
Imports System.Diagnostics
Imports System.ComponentModel

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

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

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

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

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

            Process.Start(startInfo)

            startInfo.Arguments = "www.northwindtraders.com"

            Process.Start(startInfo)
        End Sub

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

            Dim myProcess As New MyProcess()

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

注解

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

重要

使用不受信任的数据调用此方法是一种安全风险。 仅使用受信任的数据调用此方法。 有关详细信息,请参阅验证所有输入

注释

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

通过此重载,无需先创建新 Process 实例即可启动进程。 重载是创建新 Process 实例、设置 FileName 属性和 Arguments 成员 StartInfo 以及调用 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 实例相关联。 如果要创建新Process组件,而不是为现有组件启动进程,请使用其中一static个重载。 此重载和没有参数的重载都允许指定要启动的进程资源的文件名和要传递的命令行参数。

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

注释

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

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

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

另请参阅

适用于

Start(String)

Source:
Process.cs
Source:
Process.cs
Source:
Process.cs
Source:
Process.cs
Source:
Process.cs

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

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

参数

fileName
String

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

返回

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

属性

例外

打开关联的文件时出错。

-或-

找不到在该文件中指定的 fileName 文件。

进程对象已释放。

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

示例

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

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();
        }
    }
}
module processstartstatic

open System
open System.Diagnostics

// Opens the Internet Explorer application.
let openApplication (myFavoritesPath: string) =
    // Start Internet Explorer. Defaults to the home page.
    Process.Start "IExplore.exe" |> ignore

    // Display the contents of the favorites folder in the browser.
    Process.Start myFavoritesPath |> ignore

// Opens urls and .html documents using Internet Explorer.
let openWithArguments () =
    // url's are not considered documents. They can only be opened
    // by passing them as arguments.
    Process.Start("IExplore.exe", "www.northwindtraders.com") |> ignore

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

// Uses the ProcessStartInfo class to start new processes,
// both in a minimized mode.
let openWithStartInfo () =
    let startInfo = ProcessStartInfo "IExplore.exe"
    startInfo.WindowStyle <- ProcessWindowStyle.Minimized

    Process.Start startInfo |> ignore

    startInfo.Arguments <- "www.northwindtraders.com"

    Process.Start startInfo |> ignore

// Get the path that stores favorite links.
let myFavoritesPath = Environment.GetFolderPath Environment.SpecialFolder.Favorites

openApplication myFavoritesPath
openWithArguments ()
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和为Process实例调用Start的显式步骤的替代方法。

可以通过将 fileName 参数设置为最初安装应用程序的位置(例如 Web 地址)来启动 ClickOnce 应用程序。 不要通过在硬盘驱动器上指定其安装位置来启动 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 实例相关联。 如果要创建新Process组件,而不是为现有组件启动进程,请使用其中一static个重载。 此重载和没有参数的重载都允许指定要启动的进程资源的文件名。

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

注释

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

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

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

另请参阅

适用于

Start(ProcessStartInfo)

Source:
Process.cs
Source:
Process.cs
Source:
Process.cs
Source:
Process.cs
Source:
Process.cs

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

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

参数

startInfo
ProcessStartInfo

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

返回

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

属性

例外

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

-或-

参数UseShellExecute的属性startInfo也是trueRedirectStandardOutputRedirectStandardInput,或RedirectStandardError属性也是。true

-或-

参数的属性是且属性不是或为null空或Password属性不是 nullUserNametruestartInfoUseShellExecute

参数 startInfonull.

进程对象已释放。

打开关联的文件时出错。

-或-

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

-或-

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

不支持 Shell 支持的操作系统上的方法,例如 Nano Server(仅限 .NET Core)。

示例

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

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

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();
        }
    }
}
module processstartstatic

open System
open System.Diagnostics

// Opens the Internet Explorer application.
let openApplication (myFavoritesPath: string) =
    // Start Internet Explorer. Defaults to the home page.
    Process.Start "IExplore.exe" |> ignore

    // Display the contents of the favorites folder in the browser.
    Process.Start myFavoritesPath |> ignore

// Opens urls and .html documents using Internet Explorer.
let openWithArguments () =
    // url's are not considered documents. They can only be opened
    // by passing them as arguments.
    Process.Start("IExplore.exe", "www.northwindtraders.com") |> ignore

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

// Uses the ProcessStartInfo class to start new processes,
// both in a minimized mode.
let openWithStartInfo () =
    let startInfo = ProcessStartInfo "IExplore.exe"
    startInfo.WindowStyle <- ProcessWindowStyle.Minimized

    Process.Start startInfo |> ignore

    startInfo.Arguments <- "www.northwindtraders.com"

    Process.Start startInfo |> ignore

// Get the path that stores favorite links.
let myFavoritesPath = Environment.GetFolderPath Environment.SpecialFolder.Favorites

openApplication myFavoritesPath
openWithArguments ()
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扩展名。

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

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

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

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

注释

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

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

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

另请参阅

适用于

Start()

Source:
Process.cs
Source:
Process.cs
Source:
Process.cs
Source:
Process.cs
Source:
Process.cs

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

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

返回

true 如果启动进程资源,则为 false 如果未启动新进程资源(例如,如果重复使用现有进程)。

属性

例外

组件ProcessStartInfo中未指定任何文件名。

-或-

属性UseShellExecute的成员StartInfotrue while RedirectStandardInputRedirectStandardOutputRedirectStandardError istrue

打开关联文件时出错。

进程对象已释放。

不支持 Shell 支持的操作系统上的方法,例如 Nano Server(仅限 .NET Core)。

示例

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

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);
            }
        }
    }
}
open System.Diagnostics

try
    use 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() |> ignore
// 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.
with e ->
    printfn $"{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 返回。

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

重要

将此类型的实例用于不受信任的数据是一种安全风险。 仅将此对象与受信任的数据一起使用。 有关详细信息,请参阅验证所有输入

注释

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

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

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

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

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

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

注释

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

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

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

另请参阅

适用于

Start(String, IEnumerable<String>)

Source:
Process.cs
Source:
Process.cs
Source:
Process.cs
Source:
Process.cs
Source:
Process.cs

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

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

参数

fileName
String

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

arguments
IEnumerable<String>

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

返回

与进程资源关联的新 Process 项,或者 null 未启动任何进程资源。

属性

注解

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

重要

使用不受信任的数据调用此方法是一种安全风险。 仅使用受信任的数据调用此方法。 有关详细信息,请参阅验证所有输入

适用于