Process.Start 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
啟動一個流程資源並將其與元件 Process 關聯。
多載
| 名稱 | Description |
|---|---|
| Start(String, String, String, SecureString, String) |
透過指定應用程式名稱、一組命令列參數、使用者名稱、密碼及網域,啟動程序資源,並將該資源與新 Process 元件關聯。 |
| Start(String, String, SecureString, String) |
透過指定應用程式名稱、使用者名稱、密碼及網域來啟動程序資源,並將該資源與新 Process 元件關聯。 |
| Start(String, String) |
透過指定應用程式名稱及一組命令列參數來啟動程序資源,並將該資源與新 Process 元件關聯。 |
| Start(String) |
透過指定文件或應用程式檔案的名稱來啟動程序資源,並將該資源與新的 Process 元件關聯起來。 |
| Start(ProcessStartInfo) |
啟動由包含程序啟動資訊的參數指定的程序資源(例如,啟動程序的檔名),並將該資源與一個新的 Process 元件關聯。 |
| Start() | |
| Start(String, IEnumerable<String>) |
透過指定應用程式名稱及一組命令列參數來啟動程序資源。 |
Start(String, String, String, SecureString, String)
重要
此 API 不符合 CLS 規範。
透過指定應用程式名稱、一組命令列參數、使用者名稱、密碼及網域,啟動程序資源,並將該資源與新 Process 元件關聯。
public:
static System::Diagnostics::Process ^ Start(System::String ^ fileName, System::String ^ arguments, System::String ^ userName, System::Security::SecureString ^ password, System::String ^ domain);
[System.CLSCompliant(false)]
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static System.Diagnostics.Process? Start(string fileName, string arguments, string userName, System.Security.SecureString password, string domain);
[System.CLSCompliant(false)]
public static System.Diagnostics.Process Start(string fileName, string arguments, string userName, System.Security.SecureString password, string domain);
public static System.Diagnostics.Process Start(string fileName, string arguments, string userName, System.Security.SecureString password, string domain);
[<System.CLSCompliant(false)>]
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member Start : string * string * string * System.Security.SecureString * string -> System.Diagnostics.Process
[<System.CLSCompliant(false)>]
static member Start : string * string * string * System.Security.SecureString * string -> System.Diagnostics.Process
static member Start : string * string * string * System.Security.SecureString * string -> System.Diagnostics.Process
Public Shared Function Start (fileName As String, arguments As String, userName As String, password As SecureString, domain As String) As Process
參數
- fileName
- String
程序中執行的應用程式檔案名稱。
- arguments
- String
啟動程序時傳遞的命令列參數。
- userName
- String
啟動流程時要使用的使用者名稱。
- password
- SecureString
裡面 SecureString 有啟動程序時使用的密碼。
- domain
- String
啟動流程時該使用的網域。
傳回
一個與程序資源相關聯的新資源 Process ,或 null 若未啟動程序資源。 請注意,與已在執行同一程序實例同時啟動的新程序將獨立於其他程序。 此外,Start 可能會回傳一個非空程序,且 HasExited 其屬性已設定為 true。 在這種情況下,啟動程序可能啟動了自身的一個已存在實例,然後退出。
- 屬性
例外狀況
未指定檔案名稱。
開啟相關檔案時發生錯誤。
-或-
找不到該檔案 fileName 。
-或-
參數長度與完整路徑長度的總和超過 2080。 與此例外相關的錯誤訊息可能是以下其中之一:「傳送給系統呼叫的資料區域過小。」或「存取被拒絕」。
過程對象已經被處置。
此成員不支援 Linux 或 macOS(僅限 .NET Core)。
備註
利用此過載來建立新程序及其主要執行緒,透過指定其檔案名稱、命令列參數、使用者名稱、密碼與網域。 新程序接著會在指定的憑證(使用者、網域與密碼)的安全情境中執行指定的執行檔。
這很重要
使用不受信任的資料呼叫此方法存在安全風險。 只有在受信任的資料時才會呼叫此方法。 如需詳細資訊,請參閱 驗證所有輸入。
備註
當執行檔位於遠端磁碟時,必須使用統一資源識別碼(URI)來識別網路共享,而非連結磁碟代號。
備註
如果啟動的執行檔位址是 URL,則程序不會被啟動,會 null 回傳。
這種過載讓你可以在不先建立新 Process 實例的情況下啟動程序。 過載是建立新Process實例、設定FileName屬性的 、 Arguments、 UserNamePasswordDomain 、 及 屬性StartInfo,然後呼叫Start實Process例的明確步驟的替代方案。
同樣地,就像 執行 對話框可以接受有無 .exe 副檔名的執行檔名一樣,.exe 副檔名在參數中 fileName 是可選的。 例如,你可以將參數設 fileName 為「Notepad.exe」或「記事本」。 如果參數 fileName 代表一個可執行檔案,那麼該 arguments 參數可能代表一個要作用的檔案,例如中的 Notepad.exe myfile.txt文字檔。
備註
檔案名稱必須代表在超載中具有 userName、 password和 參數domain的可執行檔Start。
每當你啟動 Start 一個程序時,可能需要關閉它,否則可能會失去系統資源。 使用 CloseMainWindow 或 Kill的閉運算。 你可以透過使用程序 HasExited 的屬性來檢查程序是否已經被關閉。
適用於
Start(String, String, SecureString, String)
重要
此 API 不符合 CLS 規範。
透過指定應用程式名稱、使用者名稱、密碼及網域來啟動程序資源,並將該資源與新 Process 元件關聯。
public:
static System::Diagnostics::Process ^ Start(System::String ^ fileName, System::String ^ userName, System::Security::SecureString ^ password, System::String ^ domain);
[System.CLSCompliant(false)]
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static System.Diagnostics.Process? Start(string fileName, string userName, System.Security.SecureString password, string domain);
[System.CLSCompliant(false)]
public static System.Diagnostics.Process Start(string fileName, string userName, System.Security.SecureString password, string domain);
public static System.Diagnostics.Process Start(string fileName, string userName, System.Security.SecureString password, string domain);
[<System.CLSCompliant(false)>]
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member Start : string * string * System.Security.SecureString * string -> System.Diagnostics.Process
[<System.CLSCompliant(false)>]
static member Start : string * string * System.Security.SecureString * string -> System.Diagnostics.Process
static member Start : string * string * System.Security.SecureString * string -> System.Diagnostics.Process
Public Shared Function Start (fileName As String, userName As String, password As SecureString, domain As String) As Process
參數
- fileName
- String
程序中執行的應用程式檔案名稱。
- userName
- String
啟動流程時要使用的使用者名稱。
- password
- SecureString
裡面 SecureString 有啟動程序時使用的密碼。
- domain
- String
啟動流程時該使用的網域。
傳回
一個與程序資源相關聯的新資源 Process ,或 null 若未啟動程序資源。 請注意,與已在執行同一程序實例同時啟動的新程序將獨立於其他程序。 此外,Start 可能會回傳一個非空程序,且 HasExited 其屬性已設定為 true。 在這種情況下,啟動程序可能啟動了自身的一個已存在實例,然後退出。
- 屬性
例外狀況
未指定檔案名稱。
過程對象已經被處置。
此成員不支援 Linux 或 macOS(僅限 .NET Core)。
範例
以下程式碼範例展示了使用此超載啟動可執行檔案的方法,並示範當嘗試啟動與非可執行檔案相關的應用程式時,拋出 a 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實例、設定FileName屬性、 DomainPasswordStartInfoUserName及屬性,然後呼叫Start實Process例的明確步驟的替代方案。
同樣地,就像 執行 對話框可以接受有無 .exe 副檔名的執行檔名一樣,.exe 副檔名在參數中 fileName 是可選的。 例如,你可以將參數設 fileName 為「Notepad.exe」或「記事本」。 如果參數 fileName 代表一個可執行檔案,那麼該 arguments 參數可能代表一個要作用的檔案,例如中的 Notepad.exe myfile.txt文字檔。
備註
檔案名稱必須代表在超載中具有 userName、 password和 參數domain的可執行檔Start。
每當你啟動 Start 一個程序時,可能需要關閉它,否則可能會失去系統資源。 使用 CloseMainWindow 或 Kill的閉運算。 你可以透過使用程序 HasExited 的屬性來檢查程序是否已經被關閉。
適用於
Start(String, String)
- 來源:
- Process.cs
- 來源:
- Process.cs
- 來源:
- Process.cs
- 來源:
- Process.cs
- 來源:
- 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 可能會回傳一個非空程序,且 HasExited 其屬性已設定為 true。 在這種情況下,啟動程序可能啟動了自身的一個已存在實例,然後退出。
- 屬性
例外狀況
fileName或arguments參數為 null。
開啟相關檔案時發生錯誤。
-或-
找不到該檔案 fileName 。
-或-
參數長度與整個過程路徑長度的總和超過 2080。 與此例外相關的錯誤訊息可能是以下其中之一:「傳送給系統呼叫的資料區域過小。」或「存取被拒絕」。
過程對象已經被處置。
PATH 環境變數包含一個包含引號的字串。
範例
以下範例首先會啟動一個 Internet Explorer 實例,並在瀏覽器中顯示 Favorites 資料夾的內容。 接著它會啟動其他 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 實例、設定屬性的 和 FileNameArguments 成員 StartInfo ,以及呼叫 Start 實 Process 例的明確步驟的替代方案。
啟動程序並指定其檔案名稱和參數,類似於 Run 在 Windows Start 選單的對話框中輸入檔名和命令列參數。 因此,檔案名稱不必代表可執行檔案。 它可以是任何檔案類型,只要副檔名已與系統上安裝的應用程式相關聯。 例如,如果你有與編輯器相關的文字檔(如記事本),檔名可以有 .txt 副檔名;如果你將 .doc 檔案與文字處理工具(如 Microsoft Word)關聯,則可以用 .doc。 同樣地, Run 對話框可以接受有無 .exe 副檔名的可執行檔名,.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 網頁與伺服器控制程式碼會在網頁伺服器上的 ASP.NET 工作程序中執行。 如果你在 ASP.NET 網頁或伺服器控制中使用此 Start 方法,新的程序會在網頁伺服器上以受限權限執行。 此程序並非從與客戶端瀏覽器相同的上下文開始,且無法存取使用者桌面。
每當你啟動 Start 一個程序時,可能需要關閉它,否則可能會失去系統資源。 使用 CloseMainWindow 或 Kill的閉運算。 你可以透過使用程序 HasExited 的屬性來檢查程序是否已經被關閉。
這裡有必要說明管理討論串中公寓狀態的說明。 當 UseShellExecute 在 process true 元件的StartInfo屬性上時,請確保你在應用程式上設定了執行緒模型,方法是在方法main()上設定屬性[STAThread]。 否則,受管理執行緒可以處於某個 unknown 狀態,或置於 MTA 該狀態,後者與 UseShellExecute 衝突 true。 有些方法要求公寓狀態不一定是 unknown。 若狀態未明確設定,當應用程式遇到此類方法時,預設為 MTA,且一旦設定,公寓狀態無法更改。 然而,當 MTA 作業系統 shell 管理執行緒時,會拋出例外。
另請參閱
適用於
Start(String)
- 來源:
- Process.cs
- 來源:
- Process.cs
- 來源:
- Process.cs
- 來源:
- Process.cs
- 來源:
- 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 可能會回傳一個非空程序,且 HasExited 其屬性已設定為 true。 在這種情況下,啟動程序可能啟動了自身的一個已存在實例,然後退出。
- 屬性
例外狀況
過程對象已經被處置。
PATH 環境變數包含一個包含引號的字串。
範例
以下範例首先會啟動一個 Internet Explorer 實例,並在瀏覽器中顯示 Favorites 資料夾的內容。 接著它會啟動其他 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實例、設定StartInfoFileName屬性成員並呼叫StartProcess實例的明確步驟的替代方案。
您可以透過設定 fileName 參數為最初安裝該應用程式的地點(例如網頁地址)來啟動 ClickOnce 應用程式。 請勿透過指定 ClickOnce 應用程式在硬碟上的安裝位置來啟動它。
透過指定檔案名稱啟動程序,類似於 Run 在 Windows Start 選單的對話框中輸入資訊。 因此,檔案名稱不必代表可執行檔案。 它可以是任何檔案類型,只要副檔名已與系統上安裝的應用程式相關聯。 例如,如果你有與編輯器相關的文字檔(如記事本),檔名可以有 .txt 副檔名;如果你將 .doc 檔案與文字處理工具(如 Microsoft Word)關聯,則可以用 .doc。 同樣地, Run 對話框可以接受有無 .exe 副檔名的可執行檔名,.exe 副檔名在參數 fileName 中是可選的。 例如,你可以將參數設 fileName 為「Notepad.exe」或「記事本」。
此過載不允許命令列參數用於程序。 如果你需要為程序指定一個或多個命令列參數,請使用 Process.Start(ProcessStartInfo) or Process.Start(String, String) overloads。
與其他超載不同,該超載沒有參數的超載 Start 不是 static 成員。 當你已經建立實 Process 例並指定起始資訊(包括檔名),並且想啟動一個程序資源並將其與現有 Process 實例關聯時,使用這個超載。 當你想建立新Process元件時,使用其中一個超載工具static,而不是啟動現有元件的程序。 這種過載和沒有參數的超載都允許你指定程序資源的檔案名稱來啟動。
如果你在系統中用引號宣告了一個路徑變數,啟動該位置的任何程序時,必須完全限定該路徑。 否則,系統無法找到路徑。 例如,如果 c:\mypath 不在你的路徑上,且你用引號path = %path%;"c:\mypath"加進去:,啟動時必須完全限定任何程序。c:\mypath
備註
ASP.NET 網頁與伺服器控制程式碼會在網頁伺服器上的 ASP.NET 工作程序中執行。 如果你在 ASP.NET 網頁或伺服器控制中使用此 Start 方法,新的程序會在網頁伺服器上以受限權限執行。 此程序並非從與客戶端瀏覽器相同的上下文開始,且無法存取使用者桌面。
每當你啟動 Start 一個程序時,可能需要關閉它,否則可能會失去系統資源。 使用 CloseMainWindow 或 Kill的閉運算。 你可以透過使用程序 HasExited 的屬性來檢查程序是否已經被關閉。
這裡有必要說明管理討論串中公寓狀態的說明。 當 UseShellExecute 在 process true 元件的StartInfo屬性上時,請確保你在應用程式上設定了執行緒模型,方法是在方法main()上設定屬性[STAThread]。 否則,受管理執行緒可以處於某個 unknown 狀態,或置於 MTA 該狀態,後者與 UseShellExecute 衝突 true。 有些方法要求公寓狀態不一定是 unknown。 若狀態未明確設定,當應用程式遇到此類方法時,預設為 MTA,且一旦設定,公寓狀態無法更改。 然而,當 MTA 作業系統 shell 管理執行緒時,會拋出例外。
另請參閱
適用於
Start(ProcessStartInfo)
- 來源:
- Process.cs
- 來源:
- Process.cs
- 來源:
- Process.cs
- 來源:
- Process.cs
- 來源:
- 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
the the ProcessStartInfo 包含啟動程序所需的資訊,包括檔案名稱及任何命令列參數。
傳回
一個與程序資源相關聯的新資源 Process ,或 null 若未啟動程序資源。 請注意,與已在執行同一程序實例同時啟動的新程序將獨立於其他程序。 此外,Start 可能會回傳一個非空程序,且 HasExited 其屬性已設定為 true。 在這種情況下,啟動程序可能啟動了自身的一個已存在實例,然後退出。
- 屬性
例外狀況
參數屬性中未指定startInfoFileName檔案名稱。
-或-
參數的startInfo性質為 ,trueRedirectStandardInput且 , RedirectStandardOutput, 或RedirectStandardError性質也是 true。UseShellExecute
-或-
UseShellExecute參數的startInfo性質為true,且UserName性質不是null空,或Password性質不是null。
參數 startInfo 為 null。
過程對象已經被處置。
開啟相關檔案時發生錯誤。
-或-
找不到參數屬性中startInfoFileName指定的檔案。
-或-
參數長度與整個過程路徑長度的總和超過 2080。 與此例外相關的錯誤訊息可能是以下其中之一:「傳送給系統呼叫的資料區域過小。」或「存取被拒絕」。
此方法不支援無殼層支援的作業系統,例如 Nano Server(僅限 .NET Core)。
範例
以下範例首先會啟動一個 Internet Explorer 實例,並在瀏覽器中顯示 Favorites 資料夾的內容。 接著它會啟動其他 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並呼叫StartProcess實例的明確步驟的替代方案。
使用 ProcessStartInfo 實例作為參數,可以讓你 Start 對啟動流程時傳遞的內容有最大控制權。 如果你只需要傳遞一個檔名或檔名和參數,就不需要建立新 ProcessStartInfo 實例,雖然這也是一個選項。 唯一 Process.StartInfo 必須設定的屬性就是屬性本身 FileName 。 該 FileName 屬性不一定代表可執行檔案。 它可以是任何檔案類型,只要該副檔名與系統上已安裝的應用程式相關聯。 例如,如果你有與編輯器相關的文字檔(如記事本),該 FileName 屬性可以有 .txt 副檔名;如果你有與文字處理工具(如 Microsoft Word)關聯的 .doc 檔案,則可以有.doc副檔名。
您可以透過指定最初安裝該應用程式的地點(例如網頁地址)來啟動 ClickOnce 應用程式。 請勿透過指定 ClickOnce 應用程式在硬碟上的安裝位置來啟動它。
若ProcessStartInfo.UserName實例的 和 ProcessStartInfo.Password 屬性StartInfo已設定,則呼叫非管理CreateProcessWithLogonW函式,即使屬性值為 ProcessStartInfo.WindowStyletrue 或屬性值為 ProcessWindowStyle.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 網頁與伺服器控制程式碼會在網頁伺服器上的 ASP.NET 工作程序中執行。 如果你在 ASP.NET 網頁或伺服器控制中使用此 Start 方法,新的程序會在網頁伺服器上以受限權限執行。 此程序並非從與客戶端瀏覽器相同的上下文開始,且無法存取使用者桌面。
每當你啟動 Start 一個程序時,可能需要關閉它,否則可能會失去系統資源。 使用 CloseMainWindow 或 Kill的閉運算。 你可以透過使用程序 HasExited 的屬性來檢查程序是否已經被關閉。
這裡有必要說明管理討論串中公寓狀態的說明。 當UseShellExecute參數 在 時truestartInfo,請確保你在應用程式上設定了執行緒模型,方法是在main()方法上設定屬性[STAThread]。 否則,受管理執行緒可以處於某個 unknown 狀態,或置於 MTA 該狀態,後者與 UseShellExecute 衝突 true。 有些方法要求公寓狀態不一定是 unknown。 若狀態未明確設定,當應用程式遇到此類方法時,預設為 MTA,且一旦設定,公寓狀態無法更改。 然而,當 MTA 作業系統 shell 管理執行緒時,會拋出例外。
另請參閱
適用於
Start()
- 來源:
- Process.cs
- 來源:
- Process.cs
- 來源:
- Process.cs
- 來源:
- Process.cs
- 來源:
- Process.cs
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 若未啟動新的流程資源(例如,重複使用現有流程)。
- 屬性
例外狀況
-或-
UseShellExecute屬性的StartInfo成員是true,當 RedirectStandardInput、 RedirectStandardOutput,或RedirectStandardError為 true。
開啟相關檔案時出現錯誤。
過程對象已經被處置。
此方法不支援無殼層支援的作業系統,例如 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 回傳。
您可以透過指定最初安裝該應用程式的地點(例如網頁地址)來啟動 ClickOnce 應用程式。 請勿透過指定 ClickOnce 應用程式在硬碟上的安裝位置來啟動它。
這很重要
使用這種類型的實例與不受信任的資料存在安全風險。 此物件僅用於受信任的資料。 如需詳細資訊,請參閱 驗證所有輸入。
備註
如果你用的是 Visual Studio,這個方法的超載 Start 就是你在把 Process 元件拖到設計器後插入程式碼的那個。 利用 Properties 視窗展開 StartInfo 類別,並將適當的值寫入屬性中 FileName 。 您的變更會顯示在表格的 InitializeComponent 程序中。
這種過載 Start 不是一種 static 方法。 你必須從該類別的實例 Process 中呼叫它。 在呼叫 Start之前,您必須先為此Process實例指定StartInfo屬性資訊,因為該資訊用來決定啟動的程序資源。
方法的其他過載 Start 是 static 成員。 你不需要在呼叫方法的過載前建立元件的實例 Process 。 相反地,你可以呼叫StartProcess該類別本身,如果程序已經啟動,就會建立一個新的Process元件。 或者, null 如果某個程序被重複使用,則會回傳。 程序資源會自動與方法回傳Start的新Process元件相關聯。
StartInfo這些成員可用來複製 Windows Start 選單對話框的功能Run。 任何能輸入命令列的內容,都可以透過設定屬性中的 StartInfo 適當值來啟動。 唯一 StartInfo 必須設定的屬性就是屬性本身 FileName 。 該 FileName 屬性不一定要是可執行檔案。 它可以是任何檔案類型,只要該副檔名與系統上已安裝的應用程式相關聯。 例如,如果你有與編輯器相關的文字檔(如記事本),該 FileName 屬性可以有 .txt 副檔名;如果你有與文字處理工具(如 Microsoft Word)關聯的 .doc 檔案,則可以有.doc副檔名。
在命令列中,你可以指定特定類型的檔案要執行的動作。 例如,你可以列印文件或編輯文字檔。 請使用 Verb 屬性的 StartInfo 成員來指定這些動作。 對於其他類型的檔案,你可以在從 Run 對話框開始檔案時指定命令列參數。 例如,如果你指定瀏覽器為 FileName. 這些參數可以在屬性的Arguments成員中指定StartInfo。
如果你在系統中用引號宣告了一個路徑變數,啟動該位置的任何程序時,必須完全限定該路徑。 否則,系統無法找到路徑。 例如,如果 c:\mypath 不在你的路徑上,且你用引號path = %path%;"c:\mypath"加進去:,啟動時必須完全限定任何程序。c:\mypath
備註
ASP.NET 網頁與伺服器控制程式碼會在網頁伺服器上的 ASP.NET 工作程序中執行。 如果你在 ASP.NET 網頁或伺服器控制中使用此 Start 方法,新的程序會在網頁伺服器上以受限權限執行。 此程序並非從與客戶端瀏覽器相同的上下文開始,且無法存取使用者桌面。
每當你啟動 Start 一個程序時,可能需要關閉它,否則可能會失去系統資源。 使用 CloseMainWindow 或 Kill的閉運算。 你可以透過使用程序 HasExited 的屬性來檢查程序是否已經被關閉。
這裡有必要說明管理討論串中公寓狀態的說明。 當 UseShellExecute 在 process true 元件的StartInfo屬性上時,請確保你在應用程式上設定了執行緒模型,方法是在方法main()上設定屬性[STAThread]。 否則,受管理執行緒可以處於某個 unknown 狀態,或置於 MTA 該狀態,後者與 UseShellExecute 衝突 true。 有些方法要求公寓狀態不一定是 unknown。 若狀態未明確設定,當應用程式遇到此類方法時,預設為 MTA,且一旦設定,公寓狀態無法更改。 然而,當 MTA 作業系統 shell 管理執行緒時,會拋出例外。
另請參閱
適用於
Start(String, IEnumerable<String>)
- 來源:
- Process.cs
- 來源:
- Process.cs
- 來源:
- Process.cs
- 來源:
- Process.cs
- 來源:
- 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 若未啟動程序資源。
- 屬性
備註
每個論點如果需要都會自動跳脫。
這很重要
使用不受信任的資料呼叫此方法存在安全風險。 只有在受信任的資料時才會呼叫此方法。 如需詳細資訊,請參閱 驗證所有輸入。