Process.Id 属性

定义

获取关联进程的唯一标识符。

public:
 property int Id { int get(); };
public int Id { get; }
member this.Id : int
Public ReadOnly Property Id As Integer

属性值

Process 实例引用的、由系统生成的进程的唯一标识符。

例外

尚未设置此进程的 Id 属性。

- 或 -

没有与此 Process 对象关联的进程。

示例

以下示例演示如何获取 Id 应用程序所有正在运行的实例的 。 该代码创建记事本的新实例,列出记事本的所有实例,然后允许用户输入 Id 数字以删除特定实例。

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

        i = localByName.Length;
        while (i > 0)
        {
            Console.WriteLine("Enter a process Id to kill the process");
            string id = Console.ReadLine();
            if (string.IsNullOrEmpty(id))
                break;

            try
            {
                using (Process chosen = Process.GetProcessById(Int32.Parse(id)))
                {
                    if (chosen.ProcessName == "notepad")
                    {
                        chosen.Kill();
                        chosen.WaitForExit();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Incorrect entry.");
                continue;
            }

            i -= 1;
        }
    }
}
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

        i = localByName.Length
        While i > 0
            Console.WriteLine("Enter a process Id to kill the process")
            Dim id As String = Console.ReadLine()
            If id = String.Empty Then
                Exit While
            End If
            Try
                Using chosen As Process = Process.GetProcessById(Int32.Parse(id))
                    If chosen.ProcessName = "notepad" Then
                        chosen.Kill()
                        chosen.WaitForExit()
                    End If
                End Using
            Catch e As Exception
                Console.WriteLine("Incorrect entry.")
                GoTo ContinueWhile1
            End Try
            i -= 1
ContinueWhile1:
        End While

    End Sub
End Class

注解

如果关联的进程 Id 未运行,则进程无效。 因此,在尝试检索 Id 属性之前,应确保进程正在运行。 在进程终止之前,进程标识符会在整个系统中唯一地标识进程。

通过将进程标识符传递给 方法,可以将在本地或远程计算机上运行的进程连接到GetProcessByIdProcess实例。 GetProcessById 是一种 static 方法,用于创建新组件并自动设置 IdProcess 实例的 属性。

系统可以重复使用进程标识符。 Id仅当关联的进程正在运行时, 属性值才是唯一的。 进程终止后,系统可重用 Id 不相关的进程的属性值。

由于标识符在系统上是唯一的,因此可以将其传递给其他线程,作为传递 实例的 Process 替代方法。 此操作可以节省系统资源,同时保证正确标识进程。

适用于

另请参阅