Process.Id プロパティ

定義

関連付けられたプロセスの一意の識別子を取得します。

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

プロパティ値

Int32

この 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 必要があります。 プロセスが終了するまで、プロセス識別子はシステム全体でプロセスを一意に識別します。

ローカル コンピューターまたはリモート コンピューターで実行されているプロセスを新しい Process インスタンスに接続するには、プロセス識別子をメソッドに GetProcessById 渡します。 GetProcessById は、 static 新しいコンポーネントを作成し、新しいインスタンスの Id プロパティを自動的に設定する Process メソッドです。

プロセス識別子は、システムによって再利用できます。 Idプロパティ値は、関連付けられているプロセスの実行中にのみ一意です。 プロセスが終了した後、システムは、関連のないプロセスの Id プロパティ値を再利用できます。

識別子はシステム上で一意であるため、インスタンスを渡す代わりに他のスレッドに渡 Process すことができます。 この操作により、システム リソースを節約できますが、プロセスが正しく識別されることを保証できます。

適用対象

こちらもご覧ください