Process.GetProcessById 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
创建新的 Process 组件,并将其与指定的现有进程资源相关联。
重载
GetProcessById(Int32) |
返回一个新的 Process 组件,给定本地计算机上的进程的标识符。 |
GetProcessById(Int32, String) |
返回一个新的 Process 组件,给定一个进程标识符和网络上的计算机的名称。 |
GetProcessById(Int32)
- Source:
- Process.cs
- Source:
- Process.cs
- Source:
- 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
进程资源的系统唯一标识符。
返回
与由 processId
参数标识的本地进程资源关联的 Process 组件。
例外
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();
}
}
}
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 组件并将其与本地计算机上的进程资源相关联。 进程资源必须已存在于计算机上,因为 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)
- Source:
- Process.cs
- Source:
- Process.cs
- Source:
- 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
网络上的计算机的名称。
返回
与由 processId
参数标识的远程进程资源关联的 Process 组件。
例外
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();
}
}
}
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 组件,并将其与网络上远程计算机上的进程资源相关联。 进程资源必须已存在于指定的计算机上,因为 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 位整数)。 这是出于历史原因。