Process.GetProcessById メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
新しい Process コンポーネントを作成し、指定した既存のプロセス リソースに関連付けます。
オーバーロード
| 名前 | 説明 |
|---|---|
| GetProcessById(Int32) |
ローカル コンピューター上のプロセスの識別子を指定して、新しい Process コンポーネントを返します。 |
| GetProcessById(Int32, String) |
プロセス識別子とネットワーク上のコンピューターの名前を指定して、新しい Process コンポーネントを返します。 |
GetProcessById(Int32)
ローカル コンピューター上のプロセスの識別子を指定して、新しい 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;
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();
}
}
}
open System.Diagnostics
// Get the current process.
let currentProcess = Process.GetCurrentProcess()
// Get all processes running on the local computer.
let localAll = Process.GetProcesses()
// Get all instances of Notepad running on the local computer.
// This will return an empty array if notepad isn't running.
let 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.
let 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.
let remoteAll = Process.GetProcesses "myComputer"
// Get all instances of Notepad running on the specific computer, using machine name.
let remoteByName = Process.GetProcessesByName("notepad", "myComputer")
// Get all instances of Notepad running on the specific computer, using IP address.
let ipByName = Process.GetProcessesByName("notepad", "169.0.0.0")
// Get a process on a remote computer, using the process id and machine name.
let remoteById = Process.GetProcessById(2345, "myComputer")
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 コンポーネントを作成し、ローカル コンピューター上のプロセス リソースに関連付けます。 GetProcessById(Int32)はシステム リソースを作成せず、アプリケーションによって生成されたProcess コンポーネントにリソースを関連付けるので、プロセス リソースは既にコンピューターに存在している必要があります。 プロセス Id は、コンピューターで現在実行中のプロセスに対してのみ取得できます。 プロセスが終了した後、 GetProcessById(Int32) は期限切れの識別子を渡すと例外をスローします。
特定のコンピューターでは、プロセスの識別子は一意です。
GetProcessById(Int32) は、最大で 1 つのプロセスを返します。 特定のアプリケーションを実行しているすべてのプロセスを取得する場合は、 GetProcessesByName(String)を使用します。 指定したアプリケーションを実行しているコンピューターに複数のプロセスが存在する場合、 GetProcessesByName(String) は、関連付けられているすべてのプロセスを含む配列を返します。 これらの各プロセスの識別子に対してクエリを実行できます。 プロセス識別子は、Windows タスク マネージャーの Processes パネルで表示できます。
PID列には、プロセスに割り当てられているプロセス識別子が表示されます。
processId パラメーターは Int32 (32 ビット符号付き整数) ですが、基になるWindows API では、同様の API に対して DWORD (符号なし 32 ビット整数) が使用されます。 これは歴史的な理由によるものです。
こちらもご覧ください
適用対象
GetProcessById(Int32, String)
プロセス識別子とネットワーク上のコンピューターの名前を指定して、新しい 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;
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();
}
}
}
open System.Diagnostics
// Get the current process.
let currentProcess = Process.GetCurrentProcess()
// Get all processes running on the local computer.
let localAll = Process.GetProcesses()
// Get all instances of Notepad running on the local computer.
// This will return an empty array if notepad isn't running.
let 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.
let 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.
let remoteAll = Process.GetProcesses "myComputer"
// Get all instances of Notepad running on the specific computer, using machine name.
let remoteByName = Process.GetProcessesByName("notepad", "myComputer")
// Get all instances of Notepad running on the specific computer, using IP address.
let ipByName = Process.GetProcessesByName("notepad", "169.0.0.0")
// Get a process on a remote computer, using the process id and machine name.
let remoteById = Process.GetProcessById(2345, "myComputer")
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 コンポーネントを作成し、ネットワーク上のリモート コンピューター上のプロセス リソースに関連付けます。 GetProcessById(Int32, String)はシステム リソースを作成せず、アプリケーションによって生成されたProcess コンポーネントにリソースを関連付けるので、プロセス リソースは指定されたコンピューターに既に存在している必要があります。 プロセス Id は、コンピューターで現在実行中のプロセスに対してのみ取得できます。 プロセスが終了した後、 GetProcessById(Int32, String) は期限切れの識別子を渡すと例外をスローします。
特定のコンピューターでは、プロセスの識別子は一意です。
GetProcessById(Int32, String) は、最大で 1 つのプロセスを返します。 特定のアプリケーションを実行しているすべてのプロセスを取得する場合は、 GetProcessesByName(String)を使用します。 指定したアプリケーションを実行しているコンピューターに複数のプロセスが存在する場合、 GetProcessesByName(String) は、関連付けられているすべてのプロセスを含む配列を返します。 これらの各プロセスの識別子に対してクエリを実行できます。 プロセス識別子は、Windows タスク マネージャーの Processes パネルで表示できます。
PID列には、プロセスに割り当てられているプロセス識別子が表示されます。
machineNameを指定しない場合は、ローカル コンピューターが使用されます。 または、 machineName 値 "." または空の文字列 ("") に設定して、ローカル コンピューターを指定することもできます。
processId パラメーターは Int32 (32 ビット符号付き整数) ですが、基になるWindows API では、同様の API に対して DWORD (符号なし 32 ビット整数) が使用されます。 これは歴史的な理由によるものです。