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;
        }
    }
}
open System
open System.Diagnostics

let notePad = Process.Start "notepad"
printfn $"Started notepad process Id = {notePad.Id}"
printfn "All instances of notepad:"
// Get Process objects for all running instances on notepad.
let localByName = Process.GetProcessesByName "notepad"
let mutable i = localByName.Length

while i > 0 do
    // You can use the process Id to pass to other applications or to
    // reference that particular instance of the application.
    printfn $"{localByName.[i - 1].Id}"
    i <- i - 1

i <- localByName.Length

while i > 0 do
    printfn "Enter a process Id to kill the process"
    let id = Console.ReadLine()

    if String.IsNullOrEmpty id then
        i <- 0
    else
        try
            use chosen = Int32.Parse id |> Process.GetProcessById

            if chosen.ProcessName = "notepad" then
                chosen.Kill()
                chosen.WaitForExit()

            i <- i - 1
        with e ->
            printfn "Incorrect entry."
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 財產之前,你應該確保程序正在進行中。 在程序終止之前,程序識別碼會在整個系統中唯一識別該程序。

你可以將程序識別碼傳遞給Process方法,將正在本地或遠端電腦上執行的程序連接到新的GetProcessById實例。 GetProcessById 是一種 static 建立新元件並自動設定 IdProcess 實例屬性的方法。

系統可重複使用程序識別碼。 Id該財產價值只有在相關程序執行時才是唯一的。 程序終止後,系統可將屬性 Id 值用於無關程序。

由於該識別碼在系統中是唯一的,你可以將其傳遞給其他執行緒,作為傳遞 Process 實例的替代方案。 此舉可節省系統資源,同時確保程序被正確識別。

適用於

另請參閱