Process.GetProcessesByName 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
建立一個新 Process 元件陣列,並將其與所有共用指定程序名稱的現有程序資源關聯。
多載
| 名稱 | Description |
|---|---|
| GetProcessesByName(String) |
建立新 Process 元件的數位,並將其與共用指定進程名稱之本機計算機上的所有進程資源產生關聯。 |
| GetProcessesByName(String, String) |
建立新的 Process 元件數位,並將其與共用指定進程名稱之遠端電腦上的所有進程資源產生關聯。 |
GetProcessesByName(String)
- 來源:
- Process.cs
- 來源:
- Process.cs
- 來源:
- Process.cs
- 來源:
- Process.cs
- 來源:
- Process.cs
建立新 Process 元件的數位,並將其與共用指定進程名稱之本機計算機上的所有進程資源產生關聯。
public:
static cli::array <System::Diagnostics::Process ^> ^ GetProcessesByName(System::String ^ processName);
[System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static System.Diagnostics.Process[] GetProcessesByName(string? processName);
public static System.Diagnostics.Process[] GetProcessesByName(string? processName);
public static System.Diagnostics.Process[] GetProcessesByName(string processName);
[<System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
static member GetProcessesByName : string -> System.Diagnostics.Process[]
static member GetProcessesByName : string -> System.Diagnostics.Process[]
Public Shared Function GetProcessesByName (processName As String) As Process()
參數
- processName
- String
這個過程的親切名稱。
傳回
一個型別 Process 的陣列,代表執行指定應用程式或檔案的程序資源。
- 屬性
例外狀況
存取用於取得流程資訊的效能計數器 API 存在問題。 此例外僅限於 Windows NT、Windows 2000 及 Windows XP。
範例
以下範例檢索當前程序、本地電腦上執行的程序、所有在本機電腦上運行的記事本實例,以及本地電腦上的特定程序資訊。 接著它會從遠端電腦上取得相同程序的資訊。
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 元件陣列,並將其關聯到本地電腦上執行同一執行檔的所有程序資源。 程序資源必須已存在於電腦上,因為它 GetProcessesByName 不創造系統資源,而是將其與應用程式產生 Process 的元件關聯。
processName A 可以指定一個執行檔,該檔案目前並未在本地電腦上執行,因此該方法回傳的陣列可以是空的。
程序名稱是程序的友好名稱,例如 Outlook,且不包含 .exe 擴展或路徑。
GetProcessesByName 有助於取得並操作與同一執行檔相關的所有程序。 例如,你可以傳遞一個可執行檔名稱作為 processName 參數,以關閉該可執行檔案的所有執行實例。
雖然程序 Id 對系統中單一程序資源有獨特性,但本地電腦上的多個程序可以同時執行該 processName 參數指定的應用程式。 因此,最多 GetProcessById 回傳一個程序,但 GetProcessesByName 會回傳包含所有相關程序的陣列。 如果你需要用標準 API 呼叫操作這個程序,你可以依序查詢每個程序的識別碼。 你無法僅透過程序名稱存取程序資源,但一旦取得與程序資源相關聯的元件陣列 Process ,就可以啟動、終止及以其他方式操作系統資源。
另請參閱
適用於
GetProcessesByName(String, String)
- 來源:
- Process.cs
建立新的 Process 元件數位,並將其與共用指定進程名稱之遠端電腦上的所有進程資源產生關聯。
public:
static cli::array <System::Diagnostics::Process ^> ^ GetProcessesByName(System::String ^ processName, System::String ^ machineName);
[System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static System.Diagnostics.Process[] GetProcessesByName(string? processName, string machineName);
public static System.Diagnostics.Process[] GetProcessesByName(string? processName, string machineName);
public static System.Diagnostics.Process[] GetProcessesByName(string processName, string machineName);
[<System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
static member GetProcessesByName : string * string -> System.Diagnostics.Process[]
static member GetProcessesByName : string * string -> System.Diagnostics.Process[]
Public Shared Function GetProcessesByName (processName As String, machineName As String) As Process()
參數
- processName
- String
這個過程的親切名稱。
- machineName
- String
網路上一台電腦的名字。
傳回
一個型別 Process 的陣列,代表執行指定應用程式或檔案的程序資源。
- 屬性
例外狀況
machineName參數語法無效。 它的長度可能是零(0)。
參數 machineName 為 null。
作業系統平台不支援遠端電腦上的此功能。
嘗試連線 machineName 失敗了。
-或-
存取用於取得流程資訊的效能計數器 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 元件陣列,並將其關聯到指定電腦上執行同一執行檔的所有程序資源。 程序資源必須已存在於電腦上,因為它 GetProcessesByName 不創造系統資源,而是將其與應用程式產生 Process 的元件關聯。
processName A 可以指定一個執行檔,該檔案目前並未在本地電腦上執行,因此該方法回傳的陣列可以是空的。
程序名稱是程序的友好名稱,例如 Outlook,且不包含 .exe 擴展或路徑。
GetProcessesByName 有助於取得並操作與同一執行檔相關的所有程序。 例如,你可以傳遞一個可執行檔名稱作為 processName 參數,以關閉該可執行檔案的所有執行實例。
雖然程序 Id 對系統中單一程序資源有獨特性,但本地電腦上的多個程序可以同時執行該 processName 參數指定的應用程式。 因此,最多 GetProcessById 回傳一個程序,但 GetProcessesByName 會回傳包含所有相關程序的陣列。 如果你需要用標準 API 呼叫操作這個程序,你可以依序查詢每個程序的識別碼。 你無法僅透過程序名稱存取程序資源,但一旦取得與程序資源相關聯的元件陣列 Process ,就可以啟動、終止及以其他方式操作系統資源。
你可以利用這種過載方式,讓程序同時在本地電腦或遠端電腦上取得。 請使用「.」來指定本地電腦。 另一種過載預設使用本地電腦。
你可以存取遠端電腦上的程序,只為了查看有關這些程序的資訊,例如統計數據。 你無法關閉、終止(使用 Kill)或啟動遠端電腦的程序。