Process.GetProcessesByName 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
建立新 Process 元件的陣列,並將其與所有共用指定處理序名稱的現有處理序資源相關聯。
多載
GetProcessesByName(String, String) |
建立新 Process 元件的陣列,並將其與遠端電腦上共用指定處理序名稱的所有處理序資源相關聯。 |
GetProcessesByName(String) |
建立新 Process 元件的陣列,並將其與本機電腦上共用指定處理序名稱的所有處理序資源相關聯。 |
GetProcessesByName(String, String)
建立新 Process 元件的陣列,並將其與遠端電腦上共用指定處理序名稱的所有處理序資源相關聯。
public:
static cli::array <System::Diagnostics::Process ^> ^ GetProcessesByName(System::String ^ processName, System::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")]
public static System.Diagnostics.Process[] GetProcessesByName (string? processName, string machineName);
public static System.Diagnostics.Process[] GetProcessesByName (string processName, string machineName);
static member GetProcessesByName : string * string -> System.Diagnostics.Process[]
[<System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
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.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
int main()
{
// Get the current process.
Process^ currentProcess = Process::GetCurrentProcess();
// Get all processes running on the local computer.
array<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.
array<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.
array<Process^>^remoteAll = Process::GetProcesses("myComputer");
// Get all instances of Notepad running on the specific computer, using machine name.
array<Process^>^remoteByName = Process::GetProcessesByName( "notepad", "myComputer" );
// Get all instances of Notepad running on the specific computer, using IP address.
array<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" );
}
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();
}
}
}
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
可以針對目前未在本機計算機上執行的可執行檔指定 ,因此方法傳回的陣列可以是空的。
進程名稱是處理程式的易記名稱,例如 Outlook,不包含 .exe 擴展名或路徑。 GetProcessesByName 有助於取得及操作與相同可執行文件相關聯的所有進程。 例如,您可以傳遞可執行檔名稱做為 processName
參數,以關閉該可執行檔的所有執行中實例。
雖然進程 Id 對系統上的單一進程資源而言是唯一的,但本機計算機上的多個進程可以執行 參數所 processName
指定的應用程式。 因此, GetProcessById 最多會傳回一個進程,但 GetProcessesByName 傳回包含所有相關聯進程的陣列。 如果您需要使用標準 API 呼叫操作進程,您可以接著查詢每個進程以取得其識別碼。 您無法單獨透過行程名稱存取進程資源,但一旦擷取與進程資源相關聯的元件陣列 Process ,您就可以啟動、終止,以及操作系統資源。
您可以使用這個多載來取得本機電腦和遠端電腦上的進程。 使用 「.」 來指定本機電腦。 預設會使用本機計算機的另一個多載存在。
您只能存取遠端電腦上的進程,以檢視有關處理程式的資訊,例如統計數據。 您無法關閉、使用 Kill) 終止 (,或啟動遠端電腦上的進程。
另請參閱
適用於
GetProcessesByName(String)
- 來源:
- Process.cs
- 來源:
- Process.cs
- 來源:
- Process.cs
建立新 Process 元件的陣列,並將其與本機電腦上共用指定處理序名稱的所有處理序資源相關聯。
public:
static cli::array <System::Diagnostics::Process ^> ^ GetProcessesByName(System::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")]
public static System.Diagnostics.Process[] GetProcessesByName (string? processName);
public static System.Diagnostics.Process[] GetProcessesByName (string processName);
static member GetProcessesByName : string -> System.Diagnostics.Process[]
[<System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
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.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
int main()
{
// Get the current process.
Process^ currentProcess = Process::GetCurrentProcess();
// Get all processes running on the local computer.
array<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.
array<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.
array<Process^>^remoteAll = Process::GetProcesses("myComputer");
// Get all instances of Notepad running on the specific computer, using machine name.
array<Process^>^remoteByName = Process::GetProcessesByName( "notepad", "myComputer" );
// Get all instances of Notepad running on the specific computer, using IP address.
array<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" );
}
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();
}
}
}
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
可以針對目前未在本機計算機上執行的可執行檔指定 ,因此方法傳回的陣列可以是空的。
進程名稱是處理程式的易記名稱,例如 Outlook,不包含 .exe 擴展名或路徑。 GetProcessesByName 有助於取得及操作與相同可執行文件相關聯的所有進程。 例如,您可以傳遞可執行檔名稱做為 processName
參數,以關閉該可執行檔的所有執行中實例。
雖然進程 Id 對系統上的單一進程資源而言是唯一的,但本機計算機上的多個進程可以執行 參數所 processName
指定的應用程式。 因此, GetProcessById 最多會傳回一個進程,但 GetProcessesByName 傳回包含所有相關聯進程的陣列。 如果您需要使用標準 API 呼叫操作進程,您可以接著查詢每個進程以取得其識別碼。 您無法單獨透過行程名稱存取進程資源,但一旦擷取與進程資源相關聯的元件陣列 Process ,您就可以啟動、終止,以及操作系統資源。