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 屬性。 在進程終止之前,進程標識碼會在整個系統中唯一識別進程。

您可以將進程識別碼傳遞至 方法,將本機或遠端電腦上執行的進程連線到GetProcessById新的Process實例。 GetProcessByIdstatic 方法,可建立新的元件,並自動設定 IdProcess 實例的屬性。

系統可以重複使用進程識別碼。 Id只有在相關聯的進程正在執行時,屬性值才是唯一的。 進程終止之後,系統就可以針對不相關的進程重複使用 Id 屬性值。

因為標識碼在系統上是唯一的,所以您可以將它傳遞至其他線程,做為傳遞 Process 實例的替代方案。 此動作可節省系統資源,但仍保證已正確識別進程。

適用於

另請參閱