Process.Start 方法

定義

啟動處理序資源,並將其與 Process 元件相關聯。Starts a process resource and associates it with a 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)

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

Start(String, String, SecureString, String)

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

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

藉由指定應用程式的名稱、一組命令列引數、使用者名稱、密碼和網域來啟動處理序資源,並將該資源與新的 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()

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

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

傳回

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

例外狀況

Process 元件的 StartInfo 中未指定檔案名稱。No file name was specified in the Process component's StartInfo. 或者,StartInfo 屬性的 UseShellExecute 成員是 true,而 RedirectStandardInputRedirectStandardOutputRedirectStandardErrortrue-or- The UseShellExecute member of the StartInfo property is true while RedirectStandardInput, RedirectStandardOutput, or RedirectStandardError is true.

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

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

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

範例

下列範例會使用 Process 類別的實例來啟動處理常式。The following example uses an instance of the Process class to start a process.

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

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

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

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

您可以藉由指定原先用來安裝應用程式的位置(例如,網址)來啟動 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,在將 Process 元件拖曳至設計工具之後,Start 方法的這個多載就是您插入程式碼中的多載。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 檔案與文字處理工具(例如 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.

如果您在系統中使用引號宣告了 path 變數,則在啟動該位置中找到的任何進程時,必須完整限定該路徑。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 worker 進程內容中執行。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. UseShellExecute 在進程元件的 StartInfo 屬性上 true 時,請確定您已在應用程式上設定執行緒模型,其方式是在 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. 否則,managed 執行緒可以處於 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. 某些方法需要不 unknown的單元狀態。Some 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. 不過,當作業系統 shell 管理執行緒時,MTA 會導致擲回例外狀況。However, MTA causes an exception to be thrown when the operating system shell is managing the thread.

安全性

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

另請參閱

Start(ProcessStartInfo)

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

例外狀況

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

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

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

startInfo 參數為 nullThe startInfo parameter is null.

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

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

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

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

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

範例

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

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

#using <System.dll>

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

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

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

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

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

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

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

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

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

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

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

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

            Process.Start(startInfo);

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

            Process.Start(startInfo);
        }

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

            MyProcess myProcess = new MyProcess();

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

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

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

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

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

        ' 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 檔案與文字處理工具(例如 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.

您可以藉由指定原先用來安裝應用程式的位置(例如,網址)來啟動 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.HiddenIf the ProcessStartInfo.UserName and ProcessStartInfo.Password properties of the StartInfo instance are set, the unmanaged CreateProcessWithLogonW function is called, which starts the process in a new window even if the ProcessStartInfo.CreateNoWindow property value is true or the ProcessStartInfo.WindowStyle property value is ProcessWindowStyle.Hidden. 如果 ProcessStartInfo.Domain 屬性是 nullProcessStartInfo.UserName 屬性必須是 UPN 格式,使用者@DNS_domain_nameIf the ProcessStartInfo.Domain property is null, the ProcessStartInfo.UserName property must be in UPN format, user@DNS_domain_name.

不同于其他多載,沒有參數的 Start 的多載不是 static 成員。Unlike the other overloads, the overload of Start that has no parameters is not a static member. 當您已經建立 Process 實例、指定的開始資訊(包括檔案名),並想要啟動進程資源,並將它與現有的 Process 實例建立關聯時,請使用該多載。Use that overload when you have already created a Process instance, specified start information (including the file name), and want to start a process resource and associate it with the existing Process instance. 當您想要建立新的 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.

如果您在系統中使用引號宣告了 path 變數,則在啟動該位置中找到的任何進程時,必須完整限定該路徑。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 worker 進程內容中執行。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 startInfo parameter, make sure you have set a threading model on your application by setting the attribute [STAThread] on the main() method. 否則,managed 執行緒可以處於 unknown 狀態或進入 MTA 狀態,後者會與 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. 某些方法需要不 unknown的單元狀態。Some 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. 不過,當作業系統 shell 管理執行緒時,MTA 會導致擲回例外狀況。However, MTA causes an exception to be thrown when the operating system shell is managing the thread.

安全性

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

另請參閱

Start(String)

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

例外狀況

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

-或--or-

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

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

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

範例

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

#using <System.dll>

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

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

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

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

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

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

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

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

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

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

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

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

            Process.Start(startInfo);

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

            Process.Start(startInfo);
        }

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

            MyProcess myProcess = new MyProcess();

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

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

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

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

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

        ' 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 實例呼叫 Start 之明確步驟的替代方法。The overload is an alternative to the explicit steps of creating a new Process instance, setting the FileName member of the StartInfo property, and calling Start for the Process instance.

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

藉由指定檔案名來啟動處理常式,類似于在 Windows 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 檔案與文字處理工具(例如 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. 同樣地,如同 [Run] 對話方塊可以接受包含或不含 .exe 副檔名的可執行檔名稱一樣,.exe 副檔名在 fileName 參數中是選擇性的。Similarly, in the same way that the Run dialog box can accept an executable file name with or without the .exe extension, the .exe extension is optional in the fileName parameter. 例如,您可以將 fileName 參數設定為 "Notepad.exe" 或 "Notepad"。For example, you can set the fileName parameter to either "Notepad.exe" or "Notepad".

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

不同于其他多載,沒有參數的 Start 的多載不是 static 成員。Unlike the other overloads, the overload of Start that has no parameters is not a static member. 當您已經建立 Process 實例、指定的開始資訊(包括檔案名),並想要啟動進程資源,並將它與現有的 Process 實例建立關聯時,請使用該多載。Use that overload when you have already created a Process instance, specified start information (including the file name), and want to start a process resource and associate it with the existing Process instance. 當您想要建立新的 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.

如果您在系統中使用引號宣告了 path 變數,則在啟動該位置中找到的任何進程時,必須完整限定該路徑。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 worker 進程內容中執行。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. UseShellExecute 在進程元件的 StartInfo 屬性上 true 時,請確定您已在應用程式上設定執行緒模型,其方式是在 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. 否則,managed 執行緒可以處於 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. 某些方法需要不 unknown的單元狀態。Some 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. 不過,當作業系統 shell 管理執行緒時,MTA 會導致擲回例外狀況。However, MTA causes an exception to be thrown when the operating system shell is managing the thread.

安全性

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

另請參閱

Start(String, String)

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

例外狀況

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

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

-或--or-

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

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

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

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

範例

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

#using <System.dll>

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

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

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

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

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

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

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

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

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

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

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

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

            Process.Start(startInfo);

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

            Process.Start(startInfo);
        }

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

            MyProcess myProcess = new MyProcess();

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

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

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

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

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

        ' 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 實例呼叫 Process 之明確步驟的替代方法。The overload is an alternative to the explicit steps of creating a new Process instance, setting the FileName and Arguments members of the StartInfo property, and calling Start for the Process instance.

藉由指定檔案的檔案名和引數來啟動進程,類似于在 Windows 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 檔案與文字處理工具(例如 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. 同樣地,如同 [Run] 對話方塊可以接受包含或不含 .exe 副檔名的可執行檔名稱一樣,.exe 副檔名在 fileName 參數中是選擇性的。Similarly, in the same way that the Run dialog box can accept an executable file name with or without the .exe extension, the .exe extension is optional in the fileName parameter. 例如,您可以將 fileName 參數設定為 "Notepad.exe" 或 "Notepad"。For example, you can set the fileName parameter to either "Notepad.exe" or "Notepad". 如果 fileName 參數代表可執行檔,則 arguments 參數可能代表要採取行動的檔案,例如 Notepad.exe myfile.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, specified start information (including the file name), and 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.

如果您在系統中使用引號宣告了 path 變數,則在啟動該位置中找到的任何進程時,必須完整限定該路徑。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 worker 進程內容中執行。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. UseShellExecute 在進程元件的 StartInfo 屬性上 true 時,請確定您已在應用程式上設定執行緒模型,其方式是在 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. 否則,managed 執行緒可以處於 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. 某些方法需要不 unknown的單元狀態。Some 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. 不過,當作業系統 shell 管理執行緒時,MTA 會導致擲回例外狀況。However, MTA causes an exception to be thrown when the operating system shell is managing the thread.

安全性

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

另請參閱

Start(String, String, SecureString, String)

重要

此 API 不符合 CLS 規範。

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

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

參數

fileName
String

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

userName
String

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

password
SecureString

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

domain
String

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

傳回

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

屬性

例外狀況

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

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

-或--or-

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

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

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

範例

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

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

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

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

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

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

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

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

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

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

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

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

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

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

備註

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

注意

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

注意

如果要啟動之可執行檔的位址是 URL,則不會啟動該進程,而且會傳回 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 實例呼叫 Process 之明確步驟的替代方法。The overload is an alternative to the explicit steps of creating a new Process instance, setting the FileName, UserName, Password, and Domain properties of the StartInfo property, and calling Start for the Process instance.

同樣地,如同 [執行] 對話方塊可以接受包含或不含 .exe 副檔名的可執行檔名稱一樣,.exe 副檔名在 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.

安全性

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

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

重要

此 API 不符合 CLS 規範。

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

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

參數

fileName
String

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

arguments
String

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

userName
String

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

password
SecureString

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

domain
String

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

傳回

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

屬性

例外狀況

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

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

-或--or-

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

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

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

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

備註

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

注意

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

注意

如果要啟動之可執行檔的位址是 URL,則不會啟動該進程,而且會傳回 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 實例、設定 FileNameArgumentsUserNamePasswordDomain 屬性的 StartInfo 屬性,以及呼叫 Start 實例 Process 之明確步驟的替代方法。The overload is an alternative to the explicit steps of creating a new Process instance, setting the FileName, Arguments, UserName, Password, and Domain properties of the StartInfo property, and calling Start for the Process instance.

同樣地,如同 [執行] 對話方塊可以接受包含或不含 .exe 副檔名的可執行檔名稱一樣,.exe 副檔名在 fileName 參數中是選擇性的。Similarly, in the same way that the Run dialog box can accept an executable file name with or without the .exe extension, the .exe extension is optional in the fileName parameter. 例如,您可以將 fileName 參數設定為 "Notepad.exe" 或 "Notepad"。For example, you can set the fileName parameter to either "Notepad.exe" or "Notepad". 如果 fileName 參數代表可執行檔,則 arguments 參數可能代表要採取行動的檔案,例如 Notepad.exe myfile.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.

安全性

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

適用於