Process.GetProcesses 方法

定義

建立新 Process 元件的陣列,並將其與現有的處理序資源相關聯。

多載

GetProcesses()

為本機電腦上的每個處理序資源建立新的 Process 元件。

GetProcesses(String)

為指定電腦上的每個處理序資源建立新的 Process 元件。

GetProcesses()

來源:
Process.cs
來源:
Process.cs
來源:
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 方法來限制其數目GetProcessByIdGetProcessByIdProcess 建立元件,該元件與您傳遞至 方法的進程標識符,與系統上識別的進程相關聯。 GetProcessesByName 會建立元件數位, Process 其相關聯的進程資源會共用您傳遞至 方法的可執行檔。

備註

您可以在服務主機進程的相同實例內載入多個 Windows 服務 (svchost.exe) 。 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)

來源:
Process.cs
來源:
Process.cs
來源:
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 方法來限制其數目GetProcessByIdGetProcessByIdProcess 建立元件,該元件與您傳遞至 方法的進程標識符,與系統上識別的進程相關聯。 GetProcessesByName 會建立元件數位, Process 其相關聯的進程資源會共用您傳遞至 方法的可執行檔。

此方法的多 GetProcesses 載通常用來擷取網路上遠端電腦上執行的進程資源清單,但您可以傳遞 “.” 來指定本機計算機。

備註

您可以在服務主機進程的相同實例內載入多個 Windows 服務 (svchost.exe) 。 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