Process.GetProcessById 方法

定义

创建新的 Process 组件,并将其与指定的现有进程资源相关联。

重载

GetProcessById(Int32)

返回一个新的 Process 组件,给定本地计算机上的进程的标识符。

GetProcessById(Int32, String)

返回一个新的 Process 组件,给定一个进程标识符和网络上的计算机的名称。

GetProcessById(Int32)

Source:
Process.cs
Source:
Process.cs
Source:
Process.cs

返回一个新的 Process 组件,给定本地计算机上的进程的标识符。

C#
public static System.Diagnostics.Process GetProcessById (int processId);

参数

processId
Int32

进程资源的系统唯一标识符。

返回

与由 processId 参数标识的本地进程资源关联的 Process 组件。

例外

processId 参数指定的进程未运行。 标识符可能已过期。

示例

以下示例检索当前进程的信息、在本地计算机上运行的进程、本地计算机上运行的所有记事本实例以及本地计算机上的特定进程。 然后,它会检索远程计算机上的相同进程的信息。

C#
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();
        }
    }
}

注解

使用此方法可创建新的 Process 组件并将其与本地计算机上的进程资源相关联。 进程资源必须已存在于计算机上,因为 GetProcessById(Int32) 不会创建系统资源,而是将资源与应用程序生成的 Process 组件相关联。 只能为计算机上正在运行的进程检索进程 Id。 进程终止后,如果传递过期的标识符,GetProcessById(Int32) 将引发异常。

在任何特定计算机上,进程的标识符是唯一的。 GetProcessById(Int32) 最多返回一个进程。 如果要获取运行特定应用程序的所有进程,请使用 GetProcessesByName(String)。 如果运行指定应用程序的计算机上存在多个进程,GetProcessesByName(String) 将返回包含所有关联进程的数组。 可以轮次查询其中每个进程,以获取其标识符。 可以在 Windows 任务管理器的 Processes 面板中查看进程标识符。 PID 列显示分配给进程的进程标识符。

processId 参数是一个 Int32(32 位有符号整数),尽管基础 Windows API 对类似的 API 使用 DWORD(无符号 32 位整数)。 这是出于历史原因。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetProcessById(Int32, String)

Source:
Process.cs
Source:
Process.cs
Source:
Process.cs

返回一个新的 Process 组件,给定一个进程标识符和网络上的计算机的名称。

C#
public static System.Diagnostics.Process GetProcessById (int processId, string machineName);

参数

processId
Int32

进程资源的系统唯一标识符。

machineName
String

网络上的计算机的名称。

返回

与由 processId 参数标识的远程进程资源关联的 Process 组件。

例外

processId 参数指定的进程未运行。 标识符可能已过期。

-或-

machineName 参数语法无效。 名称的长度可能为零(0)。

machineName 参数 null

示例

以下示例检索当前进程的信息、在本地计算机上运行的进程、本地计算机上运行的所有记事本实例以及本地计算机上的特定进程。 然后,它会检索远程计算机上的相同进程的信息。

C#
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();
        }
    }
}

注解

使用此方法可以创建新的 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 位整数)。 这是出于历史原因。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1