Process.GetProcessById メソッド

定義

新しい Process コンポーネントを作成し、指定した既存のプロセス リソースに関連付けます。

オーバーロード

GetProcessById(Int32)

ローカル コンピューター上のプロセス ID が指定された新しい Process コンポーネントを返します。

GetProcessById(Int32, String)

プロセス ID とネットワーク上のコンピューターの名前が指定された新しい Process コンポーネントを返します。

GetProcessById(Int32)

ソース:
Process.cs
ソース:
Process.cs
ソース:
Process.cs

ローカル コンピューター上のプロセス ID が指定された新しい Process コンポーネントを返します。

public:
 static System::Diagnostics::Process ^ GetProcessById(int processId);
public static System.Diagnostics.Process GetProcessById (int processId);
static member GetProcessById : int -> System.Diagnostics.Process
Public Shared Function GetProcessById (processId As Integer) As Process

パラメーター

processId
Int32

システムで一意なプロセス リソースの識別子。

戻り値

processId パラメーターで識別されるローカル プロセス リソースに関連付けられた Process コンポーネント。

例外

processId パラメーターで指定されたプロセスは実行されていません。 識別子の有効期限が切れている可能性があります。

次の例では、現在のプロセス、ローカル コンピューターで実行されているプロセス、ローカル コンピューターで実行されているすべてのメモ帳のインスタンス、およびローカル コンピューター上の特定のプロセスの情報を取得します。 その後、リモート コンピューター上の同じプロセスの情報を取得します。

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
int main()
{   
   // Get the current process.    
   Process^ currentProcess = Process::GetCurrentProcess();

   // Get all processes running on the local computer.
   array<Process^>^localAll = Process::GetProcesses();

   // Get all instances of Notepad running on the local computer.
   // This will return an empty array if notepad isn't running.
   array<Process^>^localByName = Process::GetProcessesByName("notepad");

   // Get a process on the local computer, using the process id.
   // This will throw an exception if there is no such process.
   Process^ localById = Process::GetProcessById(1234);


   // Get processes running on a remote computer. Note that this
   // and all the following calls will timeout and throw an exception
   // if "myComputer" and 169.0.0.0 do not exist on your local network.

   // Get all processes on a remote computer.
   array<Process^>^remoteAll = Process::GetProcesses("myComputer");

   // Get all instances of Notepad running on the specific computer, using machine name.
   array<Process^>^remoteByName = Process::GetProcessesByName( "notepad", "myComputer" );
   
   // Get all instances of Notepad running on the specific computer, using IP address.
   array<Process^>^ipByName = Process::GetProcessesByName( "notepad", "169.0.0.0" );
   
   // Get a process on a remote computer, using the process id and machine name.
   Process^ remoteById = Process::GetProcessById( 2345, "myComputer" );
}
using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
    class MyProcess
    {
        void BindToRunningProcesses()
        {
            // Get the current process.
            Process currentProcess = Process.GetCurrentProcess();

            // Get all processes running on the local computer.
            Process[] localAll = Process.GetProcesses();

            // Get all instances of Notepad running on the local computer.
            // This will return an empty array if notepad isn't running.
            Process[] localByName = Process.GetProcessesByName("notepad");

            // Get a process on the local computer, using the process id.
            // This will throw an exception if there is no such process.
            Process localById = Process.GetProcessById(1234);

            // Get processes running on a remote computer. Note that this
            // and all the following calls will timeout and throw an exception
            // if "myComputer" and 169.0.0.0 do not exist on your local network.

            // Get all processes on a remote computer.
            Process[] remoteAll = Process.GetProcesses("myComputer");

            // Get all instances of Notepad running on the specific computer, using machine name.
            Process[] remoteByName = Process.GetProcessesByName("notepad", "myComputer");

            // Get all instances of Notepad running on the specific computer, using IP address.
            Process[] ipByName = Process.GetProcessesByName("notepad", "169.0.0.0");

            // Get a process on a remote computer, using the process id and machine name.
            Process remoteById = Process.GetProcessById(2345, "myComputer");
        }

        static void Main()
        {
            MyProcess myProcess = new MyProcess();
            myProcess.BindToRunningProcesses();
        }
    }
}
Imports System.Diagnostics
Imports System.ComponentModel

Namespace MyProcessSample
    Class MyProcess
        Sub BindToRunningProcesses()
            ' Get the current process. You can use currentProcess from this point
            ' to access various properties and call methods to control the process.
            Dim currentProcess As Process = Process.GetCurrentProcess()

            ' Get all processes running on the local computer.
            Dim localAll As Process() = Process.GetProcesses()

            ' Get all instances of Notepad running on the local computer.
            ' This will return an empty array if notepad isn't running.
            Dim localByName As Process() = Process.GetProcessesByName("notepad")

            ' Get a process on the local computer, using the process id.
            ' This will throw an exception if there is no such process.
            Dim localById As Process = Process.GetProcessById(1234)


            ' Get processes running on a remote computer. Note that this
            ' and all the following calls will timeout and throw an exception
            ' if "myComputer" and 169.0.0.0 do not exist on your local network.

            ' Get all processes on a remote computer.
            Dim remoteAll As Process() = Process.GetProcesses("myComputer")

            ' Get all instances of Notepad running on the specific computer, using machine name.
            Dim remoteByName As Process() = Process.GetProcessesByName("notepad", "myComputer")

            ' Get all instances of Notepad running on the specific computer, using IP address.
            Dim ipByName As Process() = Process.GetProcessesByName("notepad", "169.0.0.0")

            ' Get a process on a remote computer, using the process id and machine name.
            Dim remoteById As Process = Process.GetProcessById(2345, "myComputer")
        End Sub

        Shared Sub Main()
            Dim myProcess As New MyProcess()
            myProcess.BindToRunningProcesses()
        End Sub

    End Class

End Namespace 'MyProcessSample

注釈

このメソッドを使用して、新 Process しいコンポーネントを作成し、ローカル コンピューター上のプロセス リソースに関連付けます。 プロセス リソースは、システム リソースを作成するのではなく、アプリケーションによって生成されたProcessコンポーネントに関連付けるのでGetProcessById(Int32)、コンピューター上に既に存在している必要があります。 プロセス Id は、コンピューターで現在実行されているプロセスに対してのみ取得できます。 プロセスが終了した後、 GetProcessById(Int32) 有効期限が切れた識別子を渡すと例外がスローされます。

特定のコンピューターでは、プロセスの識別子は一意です。 GetProcessById(Int32) は、最大で 1 つのプロセスを返します。 特定のアプリケーションを実行しているすべてのプロセスを取得する場合は、 を使用します GetProcessesByName(String)。 指定したアプリケーションを実行しているコンピューターに複数のプロセスが存在する場合は、 GetProcessesByName(String) 関連付けられているすべてのプロセスを含む配列を返します。 これらの各プロセスの識別子については、順番にクエリを実行できます。 プロセス識別子は、Windows タスク マネージャーのパネルで Processes 表示できます。 列には PID 、プロセスに割り当てられているプロセス識別子が表示されます。

パラメーターは processIdInt32 (32 ビット符号付き整数) ですが、基になる Windows API では同様の API に (符号なし 32 ビット整数) を使用 DWORD します。 これは歴史的な理由です。

こちらもご覧ください

適用対象

GetProcessById(Int32, String)

ソース:
Process.cs
ソース:
Process.cs
ソース:
Process.cs

プロセス ID とネットワーク上のコンピューターの名前が指定された新しい Process コンポーネントを返します。

public:
 static System::Diagnostics::Process ^ GetProcessById(int processId, System::String ^ machineName);
public static System.Diagnostics.Process GetProcessById (int processId, string machineName);
static member GetProcessById : int * string -> System.Diagnostics.Process
Public Shared Function GetProcessById (processId As Integer, machineName As String) As Process

パラメーター

processId
Int32

システムで一意なプロセス リソースの識別子。

machineName
String

ネットワーク上のコンピューターの名前。

戻り値

processId パラメーターで識別されるリモート プロセス リソースに関連付けられた Process コンポーネント。

例外

processId パラメーターで指定されたプロセスは実行されていません。 識別子の有効期限が切れている可能性があります。

または

machineName パラメーターの構文が正しくありません。 名前の長さがゼロ (0) の可能性があります。

machineName パラメーターが null です。

次の例では、現在のプロセス、ローカル コンピューターで実行されているプロセス、ローカル コンピューターで実行されているすべてのメモ帳のインスタンス、およびローカル コンピューター上の特定のプロセスの情報を取得します。 その後、リモート コンピューター上の同じプロセスの情報を取得します。

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
int main()
{   
   // Get the current process.    
   Process^ currentProcess = Process::GetCurrentProcess();

   // Get all processes running on the local computer.
   array<Process^>^localAll = Process::GetProcesses();

   // Get all instances of Notepad running on the local computer.
   // This will return an empty array if notepad isn't running.
   array<Process^>^localByName = Process::GetProcessesByName("notepad");

   // Get a process on the local computer, using the process id.
   // This will throw an exception if there is no such process.
   Process^ localById = Process::GetProcessById(1234);


   // Get processes running on a remote computer. Note that this
   // and all the following calls will timeout and throw an exception
   // if "myComputer" and 169.0.0.0 do not exist on your local network.

   // Get all processes on a remote computer.
   array<Process^>^remoteAll = Process::GetProcesses("myComputer");

   // Get all instances of Notepad running on the specific computer, using machine name.
   array<Process^>^remoteByName = Process::GetProcessesByName( "notepad", "myComputer" );
   
   // Get all instances of Notepad running on the specific computer, using IP address.
   array<Process^>^ipByName = Process::GetProcessesByName( "notepad", "169.0.0.0" );
   
   // Get a process on a remote computer, using the process id and machine name.
   Process^ remoteById = Process::GetProcessById( 2345, "myComputer" );
}
using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
    class MyProcess
    {
        void BindToRunningProcesses()
        {
            // Get the current process.
            Process currentProcess = Process.GetCurrentProcess();

            // Get all processes running on the local computer.
            Process[] localAll = Process.GetProcesses();

            // Get all instances of Notepad running on the local computer.
            // This will return an empty array if notepad isn't running.
            Process[] localByName = Process.GetProcessesByName("notepad");

            // Get a process on the local computer, using the process id.
            // This will throw an exception if there is no such process.
            Process localById = Process.GetProcessById(1234);

            // Get processes running on a remote computer. Note that this
            // and all the following calls will timeout and throw an exception
            // if "myComputer" and 169.0.0.0 do not exist on your local network.

            // Get all processes on a remote computer.
            Process[] remoteAll = Process.GetProcesses("myComputer");

            // Get all instances of Notepad running on the specific computer, using machine name.
            Process[] remoteByName = Process.GetProcessesByName("notepad", "myComputer");

            // Get all instances of Notepad running on the specific computer, using IP address.
            Process[] ipByName = Process.GetProcessesByName("notepad", "169.0.0.0");

            // Get a process on a remote computer, using the process id and machine name.
            Process remoteById = Process.GetProcessById(2345, "myComputer");
        }

        static void Main()
        {
            MyProcess myProcess = new MyProcess();
            myProcess.BindToRunningProcesses();
        }
    }
}
Imports System.Diagnostics
Imports System.ComponentModel

Namespace MyProcessSample
    Class MyProcess
        Sub BindToRunningProcesses()
            ' Get the current process. You can use currentProcess from this point
            ' to access various properties and call methods to control the process.
            Dim currentProcess As Process = Process.GetCurrentProcess()

            ' Get all processes running on the local computer.
            Dim localAll As Process() = Process.GetProcesses()

            ' Get all instances of Notepad running on the local computer.
            ' This will return an empty array if notepad isn't running.
            Dim localByName As Process() = Process.GetProcessesByName("notepad")

            ' Get a process on the local computer, using the process id.
            ' This will throw an exception if there is no such process.
            Dim localById As Process = Process.GetProcessById(1234)


            ' Get processes running on a remote computer. Note that this
            ' and all the following calls will timeout and throw an exception
            ' if "myComputer" and 169.0.0.0 do not exist on your local network.

            ' Get all processes on a remote computer.
            Dim remoteAll As Process() = Process.GetProcesses("myComputer")

            ' Get all instances of Notepad running on the specific computer, using machine name.
            Dim remoteByName As Process() = Process.GetProcessesByName("notepad", "myComputer")

            ' Get all instances of Notepad running on the specific computer, using IP address.
            Dim ipByName As Process() = Process.GetProcessesByName("notepad", "169.0.0.0")

            ' Get a process on a remote computer, using the process id and machine name.
            Dim remoteById As Process = Process.GetProcessById(2345, "myComputer")
        End Sub

        Shared Sub Main()
            Dim myProcess As New MyProcess()
            myProcess.BindToRunningProcesses()
        End Sub

    End Class

End Namespace 'MyProcessSample

注釈

このメソッドを使用して、新 Process しいコンポーネントを作成し、ネットワーク上のリモート コンピューター上のプロセス リソースに関連付けます。 プロセス リソースは、システム リソースを作成するのではなく、アプリケーションによって生成されたProcessコンポーネントに関連付けるのでGetProcessById(Int32, String)、指定されたコンピューター上に既に存在している必要があります。 プロセス Id は、コンピューターで現在実行されているプロセスに対してのみ取得できます。 プロセスが終了した後、 GetProcessById(Int32, String) 有効期限が切れた識別子を渡すと例外がスローされます。

特定のコンピューターでは、プロセスの識別子は一意です。 GetProcessById(Int32, String) は、最大で 1 つのプロセスを返します。 特定のアプリケーションを実行しているすべてのプロセスを取得する場合は、 を使用します GetProcessesByName(String)。 指定したアプリケーションを実行しているコンピューターに複数のプロセスが存在する場合は、 GetProcessesByName(String) 関連付けられているすべてのプロセスを含む配列を返します。 これらの各プロセスの識別子については、順番にクエリを実行できます。 プロセス識別子は、Windows タスク マネージャーのパネルで Processes 表示できます。 列には PID 、プロセスに割り当てられているプロセス識別子が表示されます。

を指定しない場合は、 machineNameローカル コンピューターが使用されます。 または、ローカル コンピューターを指定するには、値 "." または空の文字列 ("") を設定 machineName します。

パラメーターは processIdInt32 (32 ビット符号付き整数) ですが、基になる Windows API では同様の API に (符号なし 32 ビット整数) を使用 DWORD します。 これは歴史的な理由です。

こちらもご覧ください

適用対象