Process.GetProcessById Method

Definition

Creates a new Process component, and associates it with the existing process resource that you specify.

Overloads

GetProcessById(Int32)

Returns a new Process component, given the identifier of a process on the local computer.

GetProcessById(Int32, String)

Returns a new Process component, given a process identifier and the name of a computer on the network.

GetProcessById(Int32)

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

Returns a new Process component, given the identifier of a process on the local computer.

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

Parameters

processId
Int32

The system-unique identifier of a process resource.

Returns

A Process component that is associated with the local process resource identified by the processId parameter.

Exceptions

The process specified by the processId parameter is not running. The identifier might be expired.

Examples

The following example retrieves information of the current process, processes running on the local computer, all instances of Notepad running on the local computer, and a specific process on the local computer. It then retrieves information for the same processes on a remote computer.

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

Remarks

Use this method to create a new Process component and associate it with a process resource on the local computer. The process resource must already exist on the computer, because GetProcessById(Int32) does not create a system resource, but rather associates a resource with an application-generated Process component. A process Id can be retrieved only for a process that is currently running on the computer. After the process terminates, GetProcessById(Int32) throws an exception if you pass it an expired identifier.

On any particular computer, the identifier of a process is unique. GetProcessById(Int32) returns one process at most. If you want to get all the processes running a particular application, use GetProcessesByName(String). If multiple processes exist on the computer running the specified application, GetProcessesByName(String) returns an array containing all the associated processes. You can query each of these processes in turn for its identifier. The process identifier can be viewed in the Processes panel of the Windows Task Manager. The PID column displays the process identifier that is assigned to a process.

The processId parameter is an Int32 (a 32-bit signed integer), although the underlying Windows API uses a DWORD (an unsigned 32-bit integer) for similar APIs. This is for historical reasons.

See also

Applies to

.NET 9 and other versions
Product Versions
.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

Returns a new Process component, given a process identifier and the name of a computer on the network.

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

Parameters

processId
Int32

The system-unique identifier of a process resource.

machineName
String

The name of a computer on the network.

Returns

A Process component that is associated with a remote process resource identified by the processId parameter.

Exceptions

The process specified by the processId parameter is not running. The identifier might be expired.

-or-

The machineName parameter syntax is invalid. The name might have length zero (0).

The machineName parameter is null.

Examples

The following example retrieves information of the current process, processes running on the local computer, all instances of Notepad running on the local computer, and a specific process on the local computer. It then retrieves information for the same processes on a remote computer.

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

Remarks

Use this method to create a new Process component and associate it with a process resource on a remote computer on the network. The process resource must already exist on the specified computer, because GetProcessById(Int32, String) does not create a system resource, but rather associates a resource with an application-generated Process component. A process Id can be retrieved only for a process that is currently running on the computer. After the process terminates, GetProcessById(Int32, String) throws an exception if you pass it an expired identifier.

On any particular computer, the identifier of a process is unique. GetProcessById(Int32, String) returns one process at most. If you want to get all the processes running a particular application, use GetProcessesByName(String). If multiple processes exist on the computer running the specified application, GetProcessesByName(String) returns an array containing all the associated processes. You can query each of these processes in turn for its identifier. The process identifier can be viewed in the Processes panel of the Windows Task Manager. The PID column displays the process identifier that is assigned to a process.

If you do not specify a machineName, the local computer is used. Alternatively, you can specify the local computer by setting machineName to the value "." or to an empty string ("").

The processId parameter is an Int32 (a 32-bit signed integer), although the underlying Windows API uses a DWORD (an unsigned 32-bit integer) for similar APIs. This is for historical reasons.

See also

Applies to

.NET 9 and other versions
Product Versions
.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