Process.GetProcesses 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
建立一系列新 Process 元件,並將其與現有的製程資源關聯起來。
多載
| 名稱 | Description |
|---|---|
| GetProcesses() |
為本機電腦上的每個進程資源建立新的 Process 元件。 |
| GetProcesses(String) |
為指定電腦上的每個進程資源建立新的 Process 元件。 |
GetProcesses()
為本機電腦上的每個進程資源建立新的 Process 元件。
public:
static cli::array <System::Diagnostics::Process ^> ^ GetProcesses();
public static System.Diagnostics.Process[] GetProcesses();
static member GetProcesses : unit -> System.Diagnostics.Process[]
Public Shared Function GetProcesses () As Process()
傳回
一個型別 Process 為的陣列,代表本地電腦上運行的所有程序資源。
範例
以下範例檢索當前程序、本地電腦上執行的程序、所有在本機電腦上運行的記事本實例,以及本地電腦上的特定程序資訊。 接著它會從遠端電腦上取得相同程序的資訊。
using System;
using System.Diagnostics;
using System.ComponentModel;
namespace MyProcessSample
{
class MyProcess
{
void BindToRunningProcesses()
{
// Get the current process.
Process currentProcess = Process.GetCurrentProcess();
// Get all processes running on the local computer.
Process[] localAll = Process.GetProcesses();
// Get all instances of Notepad running on the local computer.
// This will return an empty array if notepad isn't running.
Process[] localByName = Process.GetProcessesByName("notepad");
// Get a process on the local computer, using the process id.
// This will throw an exception if there is no such process.
Process localById = Process.GetProcessById(1234);
// Get processes running on a remote computer. Note that this
// and all the following calls will timeout and throw an exception
// if "myComputer" and 169.0.0.0 do not exist on your local network.
// Get all processes on a remote computer.
Process[] remoteAll = Process.GetProcesses("myComputer");
// Get all instances of Notepad running on the specific computer, using machine name.
Process[] remoteByName = Process.GetProcessesByName("notepad", "myComputer");
// Get all instances of Notepad running on the specific computer, using IP address.
Process[] ipByName = Process.GetProcessesByName("notepad", "169.0.0.0");
// Get a process on a remote computer, using the process id and machine name.
Process remoteById = Process.GetProcessById(2345, "myComputer");
}
static void Main()
{
MyProcess myProcess = new MyProcess();
myProcess.BindToRunningProcesses();
}
}
}
open System.Diagnostics
// Get the current process.
let currentProcess = Process.GetCurrentProcess()
// Get all processes running on the local computer.
let localAll = Process.GetProcesses()
// Get all instances of Notepad running on the local computer.
// This will return an empty array if notepad isn't running.
let localByName = Process.GetProcessesByName "notepad"
// Get a process on the local computer, using the process id.
// This will throw an exception if there is no such process.
let localById = Process.GetProcessById 1234
// Get processes running on a remote computer. Note that this
// and all the following calls will timeout and throw an exception
// if "myComputer" and 169.0.0.0 do not exist on your local network.
// Get all processes on a remote computer.
let remoteAll = Process.GetProcesses "myComputer"
// Get all instances of Notepad running on the specific computer, using machine name.
let remoteByName = Process.GetProcessesByName("notepad", "myComputer")
// Get all instances of Notepad running on the specific computer, using IP address.
let ipByName = Process.GetProcessesByName("notepad", "169.0.0.0")
// Get a process on a remote computer, using the process id and machine name.
let remoteById = Process.GetProcessById(2345, "myComputer")
Imports System.Diagnostics
Imports System.ComponentModel
Namespace MyProcessSample
Class MyProcess
Sub BindToRunningProcesses()
' Get the current process. You can use currentProcess from this point
' to access various properties and call methods to control the process.
Dim currentProcess As Process = Process.GetCurrentProcess()
' Get all processes running on the local computer.
Dim localAll As Process() = Process.GetProcesses()
' Get all instances of Notepad running on the local computer.
' This will return an empty array if notepad isn't running.
Dim localByName As Process() = Process.GetProcessesByName("notepad")
' Get a process on the local computer, using the process id.
' This will throw an exception if there is no such process.
Dim localById As Process = Process.GetProcessById(1234)
' Get processes running on a remote computer. Note that this
' and all the following calls will timeout and throw an exception
' if "myComputer" and 169.0.0.0 do not exist on your local network.
' Get all processes on a remote computer.
Dim remoteAll As Process() = Process.GetProcesses("myComputer")
' Get all instances of Notepad running on the specific computer, using machine name.
Dim remoteByName As Process() = Process.GetProcessesByName("notepad", "myComputer")
' Get all instances of Notepad running on the specific computer, using IP address.
Dim ipByName As Process() = Process.GetProcessesByName("notepad", "169.0.0.0")
' Get a process on a remote computer, using the process id and machine name.
Dim remoteById As Process = Process.GetProcessById(2345, "myComputer")
End Sub
Shared Sub Main()
Dim myProcess As New MyProcess()
myProcess.BindToRunningProcesses()
End Sub
End Class
End Namespace 'MyProcessSample
備註
使用此方法建立一組新 Process 元件陣列,並將其與本地電腦上的所有程序資源關聯。 程序資源必須已存在於本地電腦,因為它們 GetProcesses 不創造系統資源,而是將資源與應用程式產生 Process 的元件關聯起來。 由於作業系統本身正在執行背景程序,這個陣列永遠不會是空的。
如果你不想檢索電腦上所有執行的程序,可以用 or GetProcessesByName 方法限制它們的數量GetProcessById。 GetProcessById 建立 Process 一個元件,該元件與系統上由你傳遞給方法的程序識別碼所識別的程序相關聯。 GetProcessesByName 建立一個元件陣列 Process ,其相關程序資源共享你傳遞給方法的可執行檔案。
Note
多個Windows服務可在服務主機程序的同一實例中載入(svchost.exe)。 GetProcesses 不會識別這些個別服務;關於此點,請參見 GetServices。
另請參閱
適用於
GetProcesses(String)
為指定電腦上的每個進程資源建立新的 Process 元件。
public:
static cli::array <System::Diagnostics::Process ^> ^ GetProcesses(System::String ^ machineName);
public static System.Diagnostics.Process[] GetProcesses(string machineName);
static member GetProcesses : string -> System.Diagnostics.Process[]
Public Shared Function GetProcesses (machineName As String) As Process()
參數
- machineName
- String
電腦用來讀取程序清單。
傳回
一個型別 Process 為的陣列,代表指定電腦上所有執行的程序資源。
例外狀況
machineName參數語法無效。 它的長度可能是零(0)。
參數 machineName 為 null。
作業系統平台不支援遠端電腦上的此功能。
存取用於取得流程資訊的效能計數器 API 存在問題。 此例外僅限於 Windows NT、Windows 2000 及 Windows XP。
存取底層系統 API 時發生問題。
範例
以下範例檢索當前程序、本地電腦上執行的程序、所有在本機電腦上運行的記事本實例,以及本地電腦上的特定程序資訊。 接著它會從遠端電腦上取得相同程序的資訊。
using System;
using System.Diagnostics;
using System.ComponentModel;
namespace MyProcessSample
{
class MyProcess
{
void BindToRunningProcesses()
{
// Get the current process.
Process currentProcess = Process.GetCurrentProcess();
// Get all processes running on the local computer.
Process[] localAll = Process.GetProcesses();
// Get all instances of Notepad running on the local computer.
// This will return an empty array if notepad isn't running.
Process[] localByName = Process.GetProcessesByName("notepad");
// Get a process on the local computer, using the process id.
// This will throw an exception if there is no such process.
Process localById = Process.GetProcessById(1234);
// Get processes running on a remote computer. Note that this
// and all the following calls will timeout and throw an exception
// if "myComputer" and 169.0.0.0 do not exist on your local network.
// Get all processes on a remote computer.
Process[] remoteAll = Process.GetProcesses("myComputer");
// Get all instances of Notepad running on the specific computer, using machine name.
Process[] remoteByName = Process.GetProcessesByName("notepad", "myComputer");
// Get all instances of Notepad running on the specific computer, using IP address.
Process[] ipByName = Process.GetProcessesByName("notepad", "169.0.0.0");
// Get a process on a remote computer, using the process id and machine name.
Process remoteById = Process.GetProcessById(2345, "myComputer");
}
static void Main()
{
MyProcess myProcess = new MyProcess();
myProcess.BindToRunningProcesses();
}
}
}
open System.Diagnostics
// Get the current process.
let currentProcess = Process.GetCurrentProcess()
// Get all processes running on the local computer.
let localAll = Process.GetProcesses()
// Get all instances of Notepad running on the local computer.
// This will return an empty array if notepad isn't running.
let localByName = Process.GetProcessesByName "notepad"
// Get a process on the local computer, using the process id.
// This will throw an exception if there is no such process.
let localById = Process.GetProcessById 1234
// Get processes running on a remote computer. Note that this
// and all the following calls will timeout and throw an exception
// if "myComputer" and 169.0.0.0 do not exist on your local network.
// Get all processes on a remote computer.
let remoteAll = Process.GetProcesses "myComputer"
// Get all instances of Notepad running on the specific computer, using machine name.
let remoteByName = Process.GetProcessesByName("notepad", "myComputer")
// Get all instances of Notepad running on the specific computer, using IP address.
let ipByName = Process.GetProcessesByName("notepad", "169.0.0.0")
// Get a process on a remote computer, using the process id and machine name.
let remoteById = Process.GetProcessById(2345, "myComputer")
Imports System.Diagnostics
Imports System.ComponentModel
Namespace MyProcessSample
Class MyProcess
Sub BindToRunningProcesses()
' Get the current process. You can use currentProcess from this point
' to access various properties and call methods to control the process.
Dim currentProcess As Process = Process.GetCurrentProcess()
' Get all processes running on the local computer.
Dim localAll As Process() = Process.GetProcesses()
' Get all instances of Notepad running on the local computer.
' This will return an empty array if notepad isn't running.
Dim localByName As Process() = Process.GetProcessesByName("notepad")
' Get a process on the local computer, using the process id.
' This will throw an exception if there is no such process.
Dim localById As Process = Process.GetProcessById(1234)
' Get processes running on a remote computer. Note that this
' and all the following calls will timeout and throw an exception
' if "myComputer" and 169.0.0.0 do not exist on your local network.
' Get all processes on a remote computer.
Dim remoteAll As Process() = Process.GetProcesses("myComputer")
' Get all instances of Notepad running on the specific computer, using machine name.
Dim remoteByName As Process() = Process.GetProcessesByName("notepad", "myComputer")
' Get all instances of Notepad running on the specific computer, using IP address.
Dim ipByName As Process() = Process.GetProcessesByName("notepad", "169.0.0.0")
' Get a process on a remote computer, using the process id and machine name.
Dim remoteById As Process = Process.GetProcessById(2345, "myComputer")
End Sub
Shared Sub Main()
Dim myProcess As New MyProcess()
myProcess.BindToRunningProcesses()
End Sub
End Class
End Namespace 'MyProcessSample
備註
使用此方法建立一組新 Process 元件陣列,並將其與指定(通常是遠端)電腦上的所有流程資源關聯。 程序資源必須已存在於本地電腦,因為它們 GetProcesses 不創造系統資源,而是將資源與應用程式產生 Process 的元件關聯起來。 由於作業系統本身正在執行背景程序,這個陣列永遠不會是空的。
如果你不想檢索電腦上所有執行的程序,可以用 or GetProcessesByName 方法限制它們的數量GetProcessById。 GetProcessById 建立 Process 一個元件,該元件與系統上由你傳遞給方法的程序識別碼所識別的程序相關聯。 GetProcessesByName 建立一個元件陣列 Process ,其相關程序資源共享你傳遞給方法的可執行檔案。
這種方法的 GetProcesses 過載通常用來取得網路遠端電腦上執行的程序資源清單,但你也可以透過傳遞「.」來指定本地電腦。
Note
多個Windows服務可在服務主機程序的同一實例中載入(svchost.exe)。 GetProcesses 不會識別這些個別服務;關於此點,請參見 GetServices。