Process.GetProcessById 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
建立新的 Process 元件,並將其與您指定的現有處理序資源相關聯。
多載
GetProcessById(Int32) |
傳回新的 Process 元件,需指定本機電腦上的處理序識別項。 |
GetProcessById(Int32, String) |
傳回新的 Process 元件,需指定網路上電腦的處理序識別項和名稱。 |
GetProcessById(Int32)
- 來源:
- Process.cs
- 來源:
- Process.cs
- 來源:
- Process.cs
傳回新的 Process 元件,需指定本機電腦上的處理序識別項。
public:
static System::Diagnostics::Process ^ GetProcessById(int processId);
public static System.Diagnostics.Process GetProcessById (int processId);
static member GetProcessById : int -> System.Diagnostics.Process
Public Shared Function GetProcessById (processId As Integer) As Process
參數
- processId
- Int32
處理序資源的系統唯一識別項。
傳回
Process 元件,與 processId
參數所識別的本機處理序資源相關聯。
例外狀況
processId
參數指定的處理序未執行。 識別項可能過期。
範例
下列範例會擷取目前進程的資訊、在本機計算機上執行的進程、本機計算機上執行的所有記事本實例,以及本機計算機上的特定進程。 然後,它會擷取遠端電腦上相同進程的資訊。
#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 元件,並將它與本機計算機上的進程資源產生關聯。 進程資源必須已經存在計算機上,因為 GetProcessById(Int32) 不會建立系統資源,而是將資源與應用程式產生的 Process 元件產生關聯。 只能針對目前在計算機上執行的進程擷取進程 Id 。 進程終止之後,如果您傳遞過期的標識符, GetProcessById(Int32) 則會擲回例外狀況。
在任何特定的計算機上,進程的標識碼是唯一的。 GetProcessById(Int32) 最多會傳回一個進程。 如果您要取得執行特定應用程式的所有行程,請使用 GetProcessesByName(String)。 如果執行指定應用程式的計算機上存在多個進程,則 GetProcessesByName(String) 傳回包含所有相關聯進程的陣列。 您可以依序查詢每個處理程式的識別碼。 進程標識元可以在 Windows 任務管理員的面板中檢視 Processes
。 數據 PID
行會顯示指派給進程的進程標識碼。
參數 processId
是 Int32 (32 位帶正負號的整數) ,雖然基礎 Windows API 會針對類似的 API 使用 DWORD
不帶正負號的 32 位整數 () 。 這是基於歷史原因。
另請參閱
適用於
GetProcessById(Int32, String)
- 來源:
- Process.cs
- 來源:
- Process.cs
- 來源:
- Process.cs
傳回新的 Process 元件,需指定網路上電腦的處理序識別項和名稱。
public:
static System::Diagnostics::Process ^ GetProcessById(int processId, System::String ^ machineName);
public static System.Diagnostics.Process GetProcessById (int processId, string machineName);
static member GetProcessById : int * string -> System.Diagnostics.Process
Public Shared Function GetProcessById (processId As Integer, machineName As String) As Process
參數
- processId
- Int32
處理序資源的系統唯一識別項。
- machineName
- String
網路上的電腦名稱。
傳回
Process 元件,與 processId
參數所識別的遠端處理序資源相關聯。
例外狀況
machineName
參數為 null
。
範例
下列範例會擷取目前進程的資訊、在本機計算機上執行的進程、本機計算機上執行的所有記事本實例,以及本機計算機上的特定進程。 然後,它會擷取遠端電腦上相同進程的資訊。
#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 元件,並將它與網路上遠端計算機上的進程資源產生關聯。 進程資源必須已存在於指定的計算機上,因為 GetProcessById(Int32, String) 不會建立系統資源,而是將資源與應用程式產生的 Process 元件產生關聯。 只能針對目前在計算機上執行的進程擷取進程 Id 。 進程終止之後,如果您傳遞過期的標識符, GetProcessById(Int32, String) 則會擲回例外狀況。
在任何特定的計算機上,進程的標識碼是唯一的。 GetProcessById(Int32, String) 最多會傳回一個進程。 如果您要取得執行特定應用程式的所有行程,請使用 GetProcessesByName(String)。 如果執行指定應用程式的計算機上存在多個進程,則 GetProcessesByName(String) 傳回包含所有相關聯進程的陣列。 您可以依序查詢每個處理程式的識別碼。 進程標識元可以在 Windows 任務管理員的面板中檢視 Processes
。 數據 PID
行會顯示指派給進程的進程標識碼。
如果您未指定 machineName
,則會使用本機計算機。 或者,您可以將 設定 machineName
為值 「.」 或空字串 (“”“) 來指定本機計算機。
參數 processId
是 Int32 (32 位帶正負號的整數) ,雖然基礎 Windows API 會針對類似的 API 使用 DWORD
不帶正負號的 32 位整數 () 。 這是基於歷史原因。