下面的示例檢索的信息涉及當前進程、本地計算機上運行的“記事本”的所有實例、在使用計算機別名和 IP 地址的特定計算機上運行的“記事本”的所有實例、本地計算機和遠程計算機上運行的所有進程,以及本地計算機或遠程計算機上使用進程 ID 的特定進程。
using System; using System.Diagnostics; using System.ComponentModel; namespace MyProcessSample { /// <summary> /// Shell for the sample. /// </summary> class MyProcess { void BindToRunningProcesses() { // Get the current process. Process currentProcess = Process.GetCurrentProcess(); // Get all instances of Notepad running on the local // computer. Process [] localByName = Process.GetProcessesByName("notepad"); // Get all instances of Notepad running on the specifiec // computer. // 1. Using the computer alias (do not precede with "\\"). Process [] remoteByName = Process.GetProcessesByName("notepad", "myComputer"); // 2. Using an IP address to specify the machineName parameter. Process [] ipByName = Process.GetProcessesByName("notepad", "169.0.0.0"); // Get all processes running on the local computer. Process [] localAll = Process.GetProcesses(); // Get all processes running on the remote computer. Process [] remoteAll = Process.GetProcesses("myComputer"); // Get a process on the local computer, using the process id. Process localById = Process.GetProcessById(1234); // Get a process on a remote computer, using the process id. Process remoteById = Process.GetProcessById(2345, "myComputer"); } static void Main() { MyProcess myProcess = new MyProcess(); myProcess.BindToRunningProcesses(); } } }
Process類中幾個你可以感興趣的屬性:
名稱 說明
BasePriority 獲取關聯進程的基本優先級。
Container 獲取 IContainer,它包含 Component。(從 Component 繼承。)
EnableRaisingEvents 獲取或設置在進程終止時是否應激發 Exited 事件。
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 獲取或設置一個值,該值指示主窗口擁有焦點時是否由操作系統暫時提升關聯進程的優先級。
PriorityClass 獲取或設置關聯進程的總體優先級類別。
PrivateMemorySize 獲取專用內存大小。
PrivateMemorySize64 獲取為關聯的進程分配的專用內存量。
PrivilegedProcessorTime 獲取此進程的特權處理器時間。
ProcessName 獲取該進程的名稱。
ProcessorAffinity 獲取或設置可安排此進程中的線程在其上運行的處理器。
Responding 獲取指示進程的用戶界面當前是否響應的值。
SessionId 獲取關聯的進程的終端服務會話標識符。
Site 獲取或設置 Component 的 ISite。(從 Component 繼承。)
StandardError 獲取用於讀取應用程序錯誤輸出的流。
StandardInput 獲取用於寫入應用程序輸入的流。
StandardOutput 獲取用於讀取應用程序輸出的流。
StartInfo 獲取或設置要傳遞給 Process 的 Start 方法的屬性。
StartTime 獲取關聯進程啟動的時間。
SynchronizingObject 獲取或設置用於封送由於進程退出事件而發出的事件處理程序調用的對象。
Threads 獲取在關聯進程中運行的一組線程。
TotalProcessorTime 獲取此進程的總的處理器時間。
UserProcessorTime 獲取此進程的用戶處理器時間。
VirtualMemorySize 獲取進程的虛擬內存大小。
VirtualMemorySize64 獲取為關聯的進程分配的虛擬內存量。
WorkingSet 獲取關聯進程的物理內存使用情況。
WorkingSet64 獲取為關聯的進程分配的物理內存量。