public ref class Process : System::ComponentModel::Component, IDisposable
public ref class Process : IDisposable
public ref class Process : System::ComponentModel::Component
public class Process : System.ComponentModel.Component, IDisposable
public class Process : IDisposable
public class Process : System.ComponentModel.Component
type Process = class
inherit Component
interface IDisposable
type Process = class
interface IDisposable
type Process = class
inherit Component
Public Class Process
Inherits Component
Implements IDisposable
Public Class Process
Implements IDisposable
Public Class Process
Inherits Component
Process
using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
int main()
Process^ myProcess = gcnew 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 );
using System;
using System.Diagnostics;
using System.ComponentModel;
namespace MyProcessSample
class MyProcess
public static void Main()
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
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()
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 類別本身和靜態 Start 方法來啟動進程。
#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
// Opens the Internet Explorer application.
void OpenApplication(String^ myFavoritesPath)
// Start Internet Explorer. Defaults to the home page.
Process::Start("IExplore.exe");
// Display the contents of the favorites folder in the browser.
Process::Start(myFavoritesPath);
// Opens urls and .html documents using Internet Explorer.
void OpenWithArguments()
// URLs are not considered documents. They can only be opened
// by passing them as arguments.
Process::Start("IExplore.exe", "www.northwindtraders.com");
// Start a Web page using a browser associated with .html and .asp files.
Process::Start("IExplore.exe", "C:\\myPath\\myFile.htm");
Process::Start("IExplore.exe", "C:\\myPath\\myFile.asp");
// Uses the ProcessStartInfo class to start new processes,
// both in a minimized mode.
void OpenWithStartInfo()
ProcessStartInfo^ startInfo = gcnew ProcessStartInfo("IExplore.exe");
startInfo->WindowStyle = ProcessWindowStyle::Minimized;
Process::Start(startInfo);
startInfo->Arguments = "www.northwindtraders.com";
Process::Start(startInfo);
int main()
// Get the path that stores favorite links.
String^ myFavoritesPath = Environment::GetFolderPath(Environment::SpecialFolder::Favorites);
OpenApplication(myFavoritesPath);
OpenWithArguments();
OpenWithStartInfo();
using System;
using System.Diagnostics;
using System.ComponentModel;
namespace MyProcessSample
class MyProcess
// Opens the Internet Explorer application.
void OpenApplication(string myFavoritesPath)
// Start Internet Explorer. Defaults to the home page.
Process.Start("IExplore.exe");
// Display the contents of the favorites folder in the browser.
Process.Start(myFavoritesPath);
// Opens urls and .html documents using Internet Explorer.
void OpenWithArguments()
// url's are not considered documents. They can only be opened
// by passing them as arguments.
Process.Start("IExplore.exe", "www.northwindtraders.com");
// Start a Web page using a browser associated with .html and .asp files.
Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm");
Process.Start("IExplore.exe", "C:\\myPath\\myFile.asp");
// Uses the ProcessStartInfo class to start new processes,
// both in a minimized mode.
void OpenWithStartInfo()
ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(startInfo);
startInfo.Arguments = "www.northwindtraders.com";
Process.Start(startInfo);
static void Main()
// Get the path that stores favorite links.
string myFavoritesPath =
Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
MyProcess myProcess = new MyProcess();
myProcess.OpenApplication(myFavoritesPath);
myProcess.OpenWithArguments();
myProcess.OpenWithStartInfo();
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
下列 F# 範例會定義啟動進程、擷取所有輸出和錯誤資訊的 runProc
函式,並記錄進程已執行的毫秒數。
runProc
函式有三個參數:要啟動的應用程式名稱、要提供給應用程式的自變數,以及起始目錄。
open System
open System.Diagnostics
let runProc filename args startDir : seq<string> * seq<string> =
let timer = Stopwatch.StartNew()
let procStartInfo =
ProcessStartInfo(
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
FileName = filename,
Arguments = args
match startDir with | Some d -> procStartInfo.WorkingDirectory <- d | _ -> ()
let outputs = System.Collections.Generic.List<string>()
let errors = System.Collections.Generic.List<string>()
let outputHandler f (_sender:obj) (args:DataReceivedEventArgs) = f args.Data
use p = new Process(StartInfo = procStartInfo)
p.OutputDataReceived.AddHandler(DataReceivedEventHandler (outputHandler outputs.Add))
p.ErrorDataReceived.AddHandler(DataReceivedEventHandler (outputHandler errors.Add))
let started =
p.Start()
with | ex ->
ex.Data.Add("filename", filename)
reraise()
if not started then
failwithf "Failed to start process %s" filename
printfn "Started %s with pid %i" p.ProcessName p.Id
p.BeginOutputReadLine()
p.BeginErrorReadLine()
p.WaitForExit()
timer.Stop()
printfn "Finished %s after %A milliseconds" filename timer.ElapsedMilliseconds
let cleanOut l = l |> Seq.filter (fun o -> String.IsNullOrEmpty o |> not)
cleanOut outputs,cleanOut errors
runProc
函式的程式代碼是由 ImaginaryDevelopment 所撰寫,且可在 Microsoft Public License下取得。
Process 元件可讓您存取計算機上執行的進程。 最簡單的程式是執行中的應用程式。 線程是操作系統配置處理器時間的基本單位。 線程可以執行進程程式代碼的任何部分,包括目前正在由另一個線程執行的元件。
Process 元件是啟動、停止、控制及監視應用程式的實用工具。 您可以使用 Process 元件來取得正在執行的進程清單,也可以啟動新的進程。
Process 元件可用來存取系統進程。 初始化 Process 元件之後,它可以用來取得執行中進程的相關信息。 這類資訊包括一組線程、載入的模組(.dll 和 .exe 檔案),以及效能資訊,例如進程正在使用的記憶體數量。
此類型會實作 IDisposable 介面。 當您完成使用類型時,應該直接或間接處置它。 若要直接處置類型,請在 try
/finally
區塊中呼叫其 Dispose 方法。 若要間接處置它,請使用語言建構,例如 using
(C#) 或 Using
(在 Visual Basic 中)。 如需詳細資訊,請參閱 介面檔中的<使用實作 IDisposable 的物件>一節。
從具有不受信任數據之類別呼叫方法是安全性風險。 僅使用信任的數據,從這個類別呼叫 方法。 如需詳細資訊,請參閱 驗證所有輸入。
32 位進程無法存取 64 位進程的模組。 如果您嘗試從 32 位進程取得 64 位進程的相關信息,您將會收到 Win32Exception 例外狀況。 另一方面,64 位進程可以存取 32 位進程的模組。
進程元件會一次取得一組屬性的相關信息。
Process 元件取得任何群組一個成員的相關信息之後,它會快取該群組中其他屬性的值,直到您呼叫 Refresh 方法,才能取得群組其他成員的新資訊。 因此,屬性值不保證比最後一次呼叫 Refresh 方法還要新。 群組明細與操作系統相關。
如果您在系統中使用引號宣告路徑變數,則必須在該位置啟動任何找到的進程時,完整限定該路徑。 否則,系統將不會找到路徑。 例如,如果 c:\mypath
不在路徑中,而且您使用引號新增它:path = %path%;"c:\mypath"
,則必須在啟動 c:\mypath
時完整限定任何進程。
系統進程會依其進程標識碼在系統上唯一識別。 和許多 Windows 資源一樣,進程也會由其句柄識別,這在計算機上可能不是唯一的。 句柄是資源標識碼的泛型詞彙。 操作系統會保存進程句柄,此句柄是透過 Process 元件的 Handle 屬性存取,即使進程已結束也一樣。 因此,您可以取得進程的系統管理資訊,例如 ExitCode(通常是零成功或非零錯誤碼),以及 ExitTime。 句柄是極其有價值的資源,因此洩漏句柄比流失記憶體更致命。
這個類別包含適用於所有成員之類別層級的連結需求和繼承需求。 當立即呼叫端或衍生類別沒有完全信任許可權時,就會擲回 SecurityException。 如需安全性需求的詳細資訊,請參閱 連結需求。
.NET Core 附注
在 .NET Framework 中,Process 類別預設會針對輸入、輸出和錯誤數據流使用 Console 編碼,通常是代碼頁編碼。 例如,在文化特性為英文(美國)的系統上,代碼頁 437 是 Console 類別的預設編碼方式。 不過,.NET Core 只能提供這些編碼的有限子集。 如果是這種情況,它會使用 Encoding.UTF8 做為預設編碼。
如果 Process 物件相依於特定的代碼頁編碼,您仍然可以執行下列 ,再呼叫任何 Process 方法:
從 CodePagesEncodingProvider.Instance 屬性擷取 EncodingProvider 物件。
將 EncodingProvider 對象傳遞至 Encoding.RegisterProvider 方法,讓編碼提供者支援的其他編碼方式可供使用。
接著,Process 類別會自動使用默認系統編碼,而不是UTF8,前提是您已註冊編碼提供者,再呼叫任何 Process 方法。