Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Process Class
 Id Property

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Process..::.Id Property

Updated: September 2008

Gets the unique identifier for the associated process.

Namespace:  System.Diagnostics
Assembly:  System (in System.dll)
Visual Basic (Declaration)
Public ReadOnly Property Id As Integer
Visual Basic (Usage)
Dim instance As Process
Dim value As Integer

value = instance.Id
C#
public int Id { get; }
Visual C++
public:
property int Id {
    int get ();
}
JScript
public function get Id () : int

Property Value

Type: System..::.Int32
The system-generated unique identifier of the process that is referenced by this Process instance.
ExceptionCondition
InvalidOperationException

The process's Id property has not been set.

-or-

There is no process associated with this Process object.

PlatformNotSupportedException

The platform is Windows 98 or Windows Millennium Edition (Windows Me); set the ProcessStartInfo..::.UseShellExecute property to false to access this property on Windows 98 and Windows Me.

The process Id is not valid if the associated process is not running. Therefore, you should ensure that the process is running before attempting to retrieve the Id property. Until the process terminates, the process identifier uniquely identifies the process throughout the system.

You can connect a process that is running on a local or remote computer to a new Process instance by passing the process identifier to the GetProcessById method. GetProcessById is a static method that creates a new component and sets the Id property for the new Process instance automatically.

Process identifiers can be reused by the system. The Id property value is unique only while the associated process is running. After the process has terminated, the system can reuse the Id property value for an unrelated process.

Because the identifier is unique on the system, you can pass it to other threads as an alternative to passing a Process instance. This action can save system resources yet guarantee that the process is correctly identified.

Windows 98, Windows Millennium Edition Platform Note:

This property is not available on this platform if you started the process with ProcessStartInfo..::.UseShellExecute set to true.

The following example demonstrates how to obtain the Id for all running instances of an application. The code creates a new instance of Notepad, lists all the instances of Notepad, and then lets the user enter the Id number to remove a specific instance.

Visual Basic
Imports System
Imports System.Threading
Imports System.Security.Permissions
Imports System.Security.Principal
Imports System.Diagnostics



Class ProcessDemo

    Public Shared Sub Main()
        Dim notePad As Process = Process.Start("notepad")
        Console.WriteLine("Started notepad process Id = " + notePad.Id.ToString())
        Console.WriteLine("All instances of notepad:")
        ' Get Process objects for all running instances on notepad.
        Dim localByName As Process() = Process.GetProcessesByName("notepad")
        Dim i As Integer = localByName.Length
        While i > 0
            ' You can use the process Id to pass to other applications or to
            ' reference that particular instance of the application.
            Console.WriteLine(localByName((i - 1)).Id.ToString())
            i -= 1
        End While
        Dim chosen As Process
        i = localByName.Length
        While i > 0
            Console.WriteLine("Enter a process Id to kill the process")
            Dim id As String = Console.ReadLine()
            If id = "" Then
                Exit While
            End If
            Try
                chosen = Process.GetProcessById(Int32.Parse(id))
            Catch e As Exception
                Console.WriteLine("Incorrect entry.")
                GoTo ContinueWhile1
            End Try
            If chosen.ProcessName = "notepad" Then
                chosen.Kill()
                chosen.WaitForExit()
            End If
            i -= 1
ContinueWhile1:
        End While

    End Sub 'Main 
End Class 'ProcessDemo

C#
using System;
using System.Threading;
using System.Security.Permissions;
using System.Security.Principal;
using System.Diagnostics;

class ProcessDemo
{
    public static void Main()
    {
        Process notePad = Process.Start("notepad");
        Console.WriteLine("Started notepad process Id = " + notePad.Id);
        Console.WriteLine("All instances of notepad:");
        // Get Process objects for all running instances on notepad.
        Process[] localByName = Process.GetProcessesByName("notepad");
        int i = localByName.Length;
        while (i > 0)
        {
            // You can use the process Id to pass to other applications or to
            // reference that particular instance of the application.
            Console.WriteLine(localByName[i - 1].Id.ToString());
            i -= 1;
        }
        Process chosen;
        i = localByName.Length;
        while (i > 0)
        {
            Console.WriteLine("Enter a process Id to kill the process");
            string id = Console.ReadLine();
            if (id == "")
                break;
            try
            {
                chosen = Process.GetProcessById(Int32.Parse(id));
            }
            catch (Exception e)
            {
                Console.WriteLine("Incorrect entry.");
                continue;
            }
            if (chosen.ProcessName == "notepad")
            {
                chosen.Kill();
                chosen.WaitForExit();
            }
            i -= 1;
        }

    }
}

  • LinkDemand 

    for full trust for the immediate caller. This member cannot be used by partially trusted code.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

Date

History

Reason

September 2008

Added an example.

Customer feedback.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker