Process 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供對本機和遠端處理序的存取,並讓您能夠啟動和停止本機系統處理序。
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
- 實作
範例
下列範例會使用 類別的 Process 實例來啟動進程。
#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
int main()
{
Process^ myProcess = gcnew Process;
try
{
myProcess->StartInfo->UseShellExecute = false;
// You can start any process, HelloWorld is a do-nothing example.
myProcess->StartInfo->FileName = "C:\\HelloWorld.exe";
myProcess->StartInfo->CreateNoWindow = true;
myProcess->Start();
// This code assumes the process you are starting will terminate itself.
// Given that it is started without a window so you cannot terminate it
// on the desktop, it must terminate itself or you can do it programmatically
// from this application using the Kill method.
}
catch ( Exception^ e )
{
Console::WriteLine( e->Message );
}
}
using System;
using System.Diagnostics;
using System.ComponentModel;
namespace MyProcessSample
{
class MyProcess
{
public static void Main()
{
try
{
using (Process myProcess = new Process())
{
myProcess.StartInfo.UseShellExecute = false;
// You can start any process, HelloWorld is a do-nothing example.
myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
// This code assumes the process you are starting will terminate itself.
// Given that it is started without a window so you cannot terminate it
// on the desktop, it must terminate itself or you can do it programmatically
// from this application using the Kill method.
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
Imports System.Diagnostics
Imports System.ComponentModel
Namespace MyProcessSample
Class MyProcess
Public Shared Sub Main()
Try
Using myProcess As New Process()
myProcess.StartInfo.UseShellExecute = False
' You can start any process, HelloWorld is a do-nothing example.
myProcess.StartInfo.FileName = "C:\\HelloWorld.exe"
myProcess.StartInfo.CreateNoWindow = True
myProcess.Start()
' This code assumes the process you are starting will terminate itself.
' Given that it is started without a window so you cannot terminate it
' on the desktop, it must terminate itself or you can do it programmatically
' from this application using the Kill method.
End Using
Catch e As Exception
Console.WriteLine((e.Message))
End Try
End Sub
End Class
End Namespace
下列範例會 Process 使用 類別本身和靜態 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();
}
}
}
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 =
try
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 公開授權下取得。
備註
Process元件可讓您存取電腦上執行的進程。 最簡單的程式是執行中的應用程式。 執行緒是作業系統配置處理器時間的基本單位。 執行緒可以執行進程程式碼的任何部分,包括目前正在由另一個執行緒執行的元件。
此 Process 元件是用來啟動、停止、控制及監視應用程式的公用程式。 您可以使用 Process 元件來取得正在執行的進程清單,也可以啟動新的進程。 元件 Process 可用來存取系統進程。 在 Process 元件初始化之後,它可以用來取得執行中進程的相關資訊。 這類資訊包括執行緒集、載入的模組 (.dll 和.exe檔案) ,以及進程正在使用的記憶體數量等效能資訊。
此型別代表 IDisposable 介面。 當您完成使用型別時,您應該直接或間接處置它。 若要直接處置型別,請呼叫其 try
/finally
區塊中的 Dispose 方法。 若要間接處置它,請使用語言建構函式,例如 using
(在 C# 中) 或 Using
(在 Visual Basic 中)。 如需詳細資訊,請參閱 IDisposable 介面文章中的<使用實作 IDisposable 的物件>一節。
重要
使用不信任的資料呼叫此類別的方法,會造成安全性上的風險。 呼叫此類別的方法時,請一律使用信任的資料。 如需詳細資訊,請參閱 驗證所有輸入。
注意
32 位進程無法存取 64 位進程的模組。 如果您嘗試從 32 位進程取得 64 位進程的相關資訊,您將會收到 Win32Exception 例外狀況。 另一方面,64 位進程可以存取 32 位進程的模組。
進程元件會一次取得一組屬性的相關資訊。 Process元件取得任何群組之一成員的相關資訊之後,它會快取該群組中其他屬性的值,而且在您呼叫 Refresh 方法之前,不會取得群組其他成員的新資訊。 因此,屬性值不保證會比最後一次呼叫 Refresh 方法還要新。 群組明細與作業系統相關。
如果您在系統中使用引號宣告路徑變數,則必須在該位置啟動任何找到的進程時,完整限定該路徑。 否則,系統將不會找到路徑。 例如,如果 c:\mypath
不在路徑中,而且您使用引號新增它: path = %path%;"c:\mypath"
,則您必須在啟動它時完整限定中的任何 c:\mypath
進程。
系統進程會依其進程識別碼在系統上唯一識別。 就像許多 Windows 資源一樣,進程也會由其控制碼識別,這在電腦上可能不是唯一的。 控制碼是資源識別碼的泛型詞彙。 作業系統會保存進程控制碼,此控制碼會透過 Handle 元件的 屬性 Process 存取,即使進程已結束也一樣。 因此,您可以取得進程的系統管理資訊,例如 ExitCode (通常為零表示成功或非零錯誤碼) 和 ExitTime 。 控制碼是極有價值的資源,因此流失控制碼比流失記憶體更實用。
注意
這個類別包含套用至所有成員之類別層級的連結需求和繼承需求。 SecurityException當立即呼叫端或衍生類別沒有完全信任許可權時,就會擲回 。 如需安全性需求的詳細資訊,請參閱 連結需求。
.NET Core 附注
在.NET Framework中 Process ,類別預設會針對輸入、輸出和錯誤資料流程使用 Console 編碼,通常是字碼頁編碼。 例如,在文化特性為英文 (美國) 的系統上,字碼頁 437 是 類別的預設編碼 Console 方式。 不過,.NET Core 只能提供這些編碼的有限子集。 如果是這種情況,它會使用 Encoding.UTF8 作為預設編碼方式。
Process如果物件相依于特定的字碼頁編碼,您仍然可以在呼叫任何 Process 方法之前執行下列動作,使其可供使用:
將System.Text.Encoding.CodePages.dll元件的參考新增至您的專案。
從 屬性擷 EncodingProviderCodePagesEncodingProvider.Instance 取 物件。
將 EncodingProvider 物件傳遞至 Encoding.RegisterProvider 方法,讓編碼提供者支援其他編碼。
然後, Process 類別會自動使用預設系統編碼,而不是 UTF8,前提是您已在呼叫任何 Process 方法之前註冊編碼提供者。
建構函式
Process() |
初始化 Process 類別的新執行個體。 |
屬性
BasePriority |
取得相關處理序的基礎優先權 (Base Priority)。 |
CanRaiseEvents |
取得值,指出元件是否能引發事件。 (繼承來源 Component) |
Container |
取得包含 IContainer 的 Component。 (繼承來源 Component) |
DesignMode |
取得值,指出 Component 目前是否處於設計模式。 (繼承來源 Component) |
EnableRaisingEvents |
取得或設定是否應該在處理序終止時引發 Exited 事件。 |
Events |
取得附加在這個 Component 上的事件處理常式清單。 (繼承來源 Component) |
ExitCode |
取得相關處理序終止時指定的值。 |
ExitTime |
取得相關的處理序結束的時間。 |
Handle |
取得相關處理序的原生控制代碼。 |
HandleCount |
取得處理序開啟的控制代碼數目。 |
HasExited |
取得值,指出相關的處理序是否已經終止。 |
Id |
取得相關處理序的唯一識別項。 |
MachineName |
取得相關處理序正在執行的所在電腦的名稱。 |
MainModule |
取得相關處理序的主要模組。 |
MainWindowHandle |
取得相關處理序主視窗的視窗控制代碼。 |
MainWindowTitle |
取得處理序的主視窗標題。 |
MaxWorkingSet |
取得或設定關聯的處理序所允許的工作集大小上限,以位元組為單位。 |
MinWorkingSet |
取得或設定關聯的處理序所允許的工作集大小下限,以位元組為單位。 |
Modules |
取得相關的處理序所載入的模組。 |
NonpagedSystemMemorySize |
已過時。
已過時。
已過時。
已過時。
取得配置給關聯處理序的未分頁系統記憶體量 (以位元組為單位)。 |
NonpagedSystemMemorySize64 |
取得配置給關聯處理序的未分頁系統記憶體量 (以位元組為單位)。 |
PagedMemorySize |
已過時。
已過時。
已過時。
已過時。
取得配置給關聯處理序的分頁記憶體量 (以位元組為單位)。 |
PagedMemorySize64 |
取得配置給關聯處理序的分頁記憶體量 (以位元組為單位)。 |
PagedSystemMemorySize |
已過時。
已過時。
已過時。
已過時。
取得配置給關聯處理序的可分頁系統記憶體量 (以位元組為單位)。 |
PagedSystemMemorySize64 |
取得配置給關聯處理序的可分頁系統記憶體量 (以位元組為單位)。 |
PeakPagedMemorySize |
已過時。
已過時。
已過時。
已過時。
取得關聯處理序所使用之虛擬記憶體分頁檔的最大記憶體量 (以位元組為單位)。 |
PeakPagedMemorySize64 |
取得關聯處理序所使用之虛擬記憶體分頁檔的最大記憶體量 (以位元組為單位)。 |
PeakVirtualMemorySize |
已過時。
已過時。
已過時。
已過時。
取得關聯處理序所使用最大虛擬記憶體量 (以位元組為單位)。 |
PeakVirtualMemorySize64 |
取得關聯處理序所使用最大虛擬記憶體量 (以位元組為單位)。 |
PeakWorkingSet |
已過時。
已過時。
已過時。
已過時。
取得相關處理序工作集大小的最大值 (位元組)。 |
PeakWorkingSet64 |
取得關聯處理序所使用最大實體記憶體量 (以位元組為單位)。 |
PriorityBoostEnabled |
取得或設定值,指出作業系統是否應該在主視窗有焦點 (Focus) 時,暫時提高相關的處理序優先權。 |
PriorityClass |
取得或設定相關處理序的整體優先權分類。 |
PrivateMemorySize |
已過時。
已過時。
已過時。
已過時。
取得配置給關聯處理序的私用記憶體量 (以位元組為單位)。 |
PrivateMemorySize64 |
取得配置給關聯處理序的私用記憶體量 (以位元組為單位)。 |
PrivilegedProcessorTime |
取得這個處理序使用處理器的授權時間。 |
ProcessName |
取得處理序的名稱。 |
ProcessorAffinity |
取得或設定處理器,這個處理序中的執行緒可以在其上排程執行。 |
Responding |
取得值,指出處理序的使用者介面是否正在回應。 |
SafeHandle |
取得這個處理序的原生控制代碼。 |
SessionId |
取得相關處理序的終端機服務工作階段識別項。 |
Site | (繼承來源 Component) |
StandardError |
取得用來讀取應用程式錯誤輸出的資料流。 |
StandardInput |
取得用來寫入應用程式輸入的資料流。 |
StandardOutput |
取得用來讀取應用程式文字輸出的資料流。 |
StartInfo | |
StartTime |
取得相關的處理序啟動的時間。 |
SynchronizingObject |
取得或設定物件,用以封送處理因處理序結束事件而發出的事件處理常式呼叫。 |
Threads |
取得正在相關的處理序中執行的執行緒集。 |
TotalProcessorTime |
取得這個處理序的總處理器時間。 |
UserProcessorTime |
取得這個處理序的使用者處理器時間。 |
VirtualMemorySize |
已過時。
已過時。
已過時。
已過時。
取得處理序的虛擬記憶體的大小 (以位元組為單位)。 |
VirtualMemorySize64 |
取得配置給關聯處理序的虛擬記憶體量,以位元組為單位。 |
WorkingSet |
已過時。
已過時。
已過時。
已過時。
取得相關處理序的實體記憶體使用量 (以位元組為單位)。 |
WorkingSet64 |
取得配置給關聯處理序的實體記憶體量,以位元組為單位。 |
方法
BeginErrorReadLine() |
在應用程式的重新導向 StandardError 資料流上開始非同步讀取作業。 |
BeginOutputReadLine() |
在應用程式的重新導向 StandardOutput 資料流上開始非同步讀取作業。 |
CancelErrorRead() |
在應用程式的重新導向 StandardError 資料流上取消非同步讀取作業。 |
CancelOutputRead() |
在應用程式的重新導向 StandardOutput 資料流上取消非同步讀取作業。 |
Close() |
釋放與這個元件相關的所有資源。 |
CloseMainWindow() |
關閉有使用者介面的處理序,方法是傳送關閉訊息至其主視窗。 |
CreateObjRef(Type) |
建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。 (繼承來源 MarshalByRefObject) |
Dispose() |
執行與釋放 (Free)、釋放 (Release) 或重設 Unmanaged 資源相關聯之應用程式定義的工作。 |
Dispose() |
釋放 Component 所使用的所有資源。 (繼承來源 Component) |
Dispose(Boolean) |
釋放這個處理序使用的所有資源。 |
EnterDebugMode() |
啟用目前執行緒上的原生屬性 |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetCurrentProcess() |
取得新的 Process 元件,並將其與目前現用處理序相關聯。 |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetLifetimeService() |
已過時。
擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。 (繼承來源 MarshalByRefObject) |
GetProcessById(Int32) |
傳回新的 Process 元件,需指定本機電腦上的處理序識別項。 |
GetProcessById(Int32, String) |
傳回新的 Process 元件,需指定網路上電腦的處理序識別項和名稱。 |
GetProcesses() |
為本機電腦上的每個處理序資源建立新的 Process 元件。 |
GetProcesses(String) |
為指定電腦上的每個處理序資源建立新的 Process 元件。 |
GetProcessesByName(String) |
建立新 Process 元件的陣列,並將其與本機電腦上共用指定處理序名稱的所有處理序資源相關聯。 |
GetProcessesByName(String, String) |
建立新 Process 元件的陣列,並將其與遠端電腦上共用指定處理序名稱的所有處理序資源相關聯。 |
GetService(Type) |
傳回表示 Component 或其 Container 所提供之服務的物件。 (繼承來源 Component) |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
InitializeLifetimeService() |
已過時。
取得存留期服務物件,以控制這個執行個體的存留期原則。 (繼承來源 MarshalByRefObject) |
Kill() |
立即停止相關的處理序。 |
Kill(Boolean) |
立即停止關聯的處理序,並選擇性地停止其子處理序。 |
LeaveDebugMode() |
讓 Process 元件離開可與使用特殊模式執行的作業系統處理序互動的狀態。 |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
MemberwiseClone(Boolean) |
建立目前 MarshalByRefObject 物件的淺層複本。 (繼承來源 MarshalByRefObject) |
OnExited() |
引發 Exited 事件。 |
Refresh() |
捨棄快取於處理序元件內之相關處理序的任何資訊。 |
Start() | |
Start(ProcessStartInfo) |
啟動含有處理序啟動資訊 (例如,要啟動之處理序的檔名) 的參數所指定的處理序資源,並將該資源與新的 Process 元件相關聯。 |
Start(String) |
藉由指定文件或應用程式檔案的名稱啟動處理序資源,並將該資源與新的 Process 元件相關聯。 |
Start(String, IEnumerable<String>) |
藉由指定應用程式的名稱和一組命令列引數來啟動處理序資源。 |
Start(String, String) |
藉由指定應用程式的名稱和一組命令列引數來啟動處理序資源,並將該資源與新的 Process 元件相關聯。 |
Start(String, String, SecureString, String) |
藉由指定應用程式的名稱、使用者名稱、密碼和網域來啟動處理序資源,並將該資源與新的 Process 元件相關聯。 |
Start(String, String, String, SecureString, String) |
藉由指定應用程式的名稱、一組命令列引數、使用者名稱、密碼和網域來啟動處理序資源,並將該資源與新的 Process 元件相關聯。 |
ToString() |
將處理序的名稱格式化為字串,如果適用,將它和父代 (Parent) 元件類型結合。 |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |
WaitForExit() |
指示 Process 元件無限期等候相關聯處理序結束。 |
WaitForExit(Int32) |
指示 Process 元件等候相關聯處理序結束的指定毫秒數。 |
WaitForExit(TimeSpan) |
指示進程元件等候指定的時間量,讓相關聯的進程結束。 |
WaitForExitAsync(CancellationToken) |
指示處理序元件等候相關聯的處理序結束,或等候 |
WaitForInputIdle() |
讓 Process 元件無限期等候相關聯處理序進入閒置狀態。 這個多載只適用於具有使用者介面和訊息迴圈的處理序。 |
WaitForInputIdle(Int32) |
讓 Process 元件等候相關聯處理序進入閒置狀態的指定毫秒數。 這個多載只適用於具有使用者介面和訊息迴圈的處理序。 |
WaitForInputIdle(TimeSpan) |
Process讓元件等候指定的 |
事件
Disposed |
當 Dispose() 方法的呼叫處置元件時,就會發生。 (繼承來源 Component) |
ErrorDataReceived |
發生於應用程式寫入至其重新導向的 StandardError 資料流時。 |
Exited |
發生於處理序結束時。 |
OutputDataReceived |
發生於應用程式將某行寫入至其重新導向的 StandardOutput 資料流時。 |