Process.GetProcessById 方法

定義

建立一個新 Process 元件,並將其與你指定的現有流程資源關聯。

多載

名稱 Description
GetProcessById(Int32)

傳回新的 Process 元件,指定本機計算機上的進程標識碼。

GetProcessById(Int32, String)

傳回新的 Process 元件,指定網路上計算機的進程標識碼和名稱。

GetProcessById(Int32)

來源:
Process.cs
來源:
Process.cs
來源:
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;
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)

來源:
Process.cs
來源:
Process.cs
來源:
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 參數識別。

例外狀況

參數 processId 指定的程序並未執行。 識別碼可能已過期。

-或-

machineName參數語法無效。 名稱的長度可能為零(0)。

參數 machineNamenull

範例

以下範例檢索當前程序、本地電腦上執行的程序、所有在本機電腦上運行的記事本實例,以及本地電腦上的特定程序資訊。 接著它會從遠端電腦上取得相同程序的資訊。

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 位元整數)。 這是出於歷史考量。

另請參閱

適用於