Process.GetProcesses 方法

定义

创建新的 Process 组件的数组,并将它们与现有进程资源关联。

重载

GetProcesses()

为本地计算机上的每个进程资源创建一个新的 Process 组件。

GetProcesses(String)

为指定计算机上的每个进程资源创建一个新的 Process 组件。

GetProcesses()

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

为本地计算机上的每个进程资源创建一个新的 Process 组件。

C#
public static System.Diagnostics.Process[] GetProcesses ();
C#
[System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static System.Diagnostics.Process[] GetProcesses ();

返回

类型 Process 的数组,表示本地计算机上运行的所有进程资源。

属性

示例

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

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 组件构成的数组,并将其与本地计算机上的所有进程资源相关联。 进程资源必须已存在于本地计算机上,因为 GetProcesses 不会创建系统资源,而是将资源与应用程序生成的 Process 组件相关联。 由于操作系统本身正在运行后台进程,因此此数组永远不会为空。

如果不想检索计算机上运行的所有进程,可以使用 或 GetProcessesByName 方法限制其数量GetProcessByIdGetProcessById 创建一个 Process 组件,该组件与系统上由传递给 方法的进程标识符标识的进程相关联。 GetProcessesByName 创建一个组件数组, Process 这些组件的关联进程资源共享传递给 方法的可执行文件。

备注

可以在服务主机进程 (svchost.exe) 的同一实例中加载多个 Windows 服务。 GetProcesses 不识别这些单独的服务;有关,请参阅 GetServices

另请参阅

适用于

.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

GetProcesses(String)

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

为指定计算机上的每个进程资源创建一个新的 Process 组件。

C#
public static System.Diagnostics.Process[] GetProcesses (string machineName);
C#
[System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static System.Diagnostics.Process[] GetProcesses (string machineName);

参数

machineName
String

从其中读取进程列表的计算机。

返回

类型 Process 的数组,表示指定计算机上运行的所有进程资源。

属性

例外

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

machineName 参数为 null

操作系统平台不支持在远程计算机上进行此操作。

访问用于获取进程信息的性能计数器 API 时遇到问题。 此异常特定于 Windows NT、Windows 2000 和 Windows XP。

访问基础系统 API 时出现问题。

示例

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

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 组件构成的数组,并将其与指定 (通常是远程) 计算机上的所有进程资源相关联。 进程资源必须已存在于本地计算机上,因为 GetProcesses 不会创建系统资源,而是将资源与应用程序生成的 Process 组件相关联。 由于操作系统本身正在运行后台进程,因此此数组永远不会为空。

如果不想检索计算机上运行的所有进程,可以使用 或 GetProcessesByName 方法限制其数量GetProcessByIdGetProcessById 创建一个 Process 组件,该组件与系统上由传递给 方法的进程标识符标识的进程相关联。 GetProcessesByName 创建一个组件数组, Process 这些组件的关联进程资源共享传递给 方法的可执行文件。

方法的 GetProcesses 此重载通常用于检索网络上远程计算机上运行的进程资源列表,但可以通过传递“.”来指定本地计算机。

备注

可以在服务主机进程 (svchost.exe) 的同一实例中加载多个 Windows 服务。 GetProcesses 不识别这些单独的服务;有关,请参阅 GetServices

另请参阅

适用于

.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