Process.GetProcesses メソッド

定義

新しい Process コンポーネントの配列を作成し、それらを既存のプロセス リソースに関連付けます。

オーバーロード

名前 説明
GetProcesses()

ローカル コンピューター上のプロセス リソースごとに新しい Process コンポーネントを作成します。

GetProcesses(String)

指定したコンピューター上のプロセス リソースごとに新しい Process コンポーネントを作成します。

GetProcesses()

ローカル コンピューター上のプロセス リソースごとに新しい Process コンポーネントを作成します。

public:
 static cli::array <System::Diagnostics::Process ^> ^ GetProcesses();
public static System.Diagnostics.Process[] GetProcesses();
static member GetProcesses : unit -> System.Diagnostics.Process[]
Public Shared Function GetProcesses () As Process()

返品

ローカル コンピューターで実行されているすべてのプロセス リソースを表す Process 型の配列。

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

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 コンポーネントの配列を作成し、ローカル コンピューター上のすべてのプロセス リソースに関連付けます。 GetProcessesはシステム リソースを作成せず、アプリケーションによって生成されたProcess コンポーネントにリソースを関連付けるので、プロセス リソースはローカル コンピューターに既に存在している必要があります。 オペレーティング システム自体はバックグラウンド プロセスを実行しているため、この配列は空ではありません。

コンピューター上で実行されているすべてのプロセスを取得しない場合は、 GetProcessById または GetProcessesByName メソッドを使用して、その数を制限できます。 GetProcessById は、メソッドに渡すプロセス識別子によってシステムで識別されるプロセスに関連付けられている Process コンポーネントを作成します。 GetProcessesByName は、関連付けられたプロセス リソースがメソッドに渡す実行可能ファイルを共有する Process コンポーネントの配列を作成します。

Note

サービス ホスト プロセス (svchost.exe) の同じインスタンス内に複数のWindows サービスを読み込むことができます。 GetProcesses はこれらの個々のサービスを識別しません。その場合は、 GetServicesを参照してください。

こちらもご覧ください

適用対象

GetProcesses(String)

指定したコンピューター上のプロセス リソースごとに新しい Process コンポーネントを作成します。

public:
 static cli::array <System::Diagnostics::Process ^> ^ GetProcesses(System::String ^ machineName);
public static System.Diagnostics.Process[] GetProcesses(string machineName);
static member GetProcesses : string -> System.Diagnostics.Process[]
Public Shared Function GetProcesses (machineName As String) As Process()

パラメーター

machineName
String

プロセスの一覧を読み取るコンピューター。

返品

指定したコンピューターで実行されているすべてのプロセス リソースを表す Process 型の配列。

例外

machineName パラメーターの構文が無効です。 長さが 0 の場合があります。

machineName パラメーターはnull

オペレーティング システム プラットフォームは、リモート コンピューターではこの操作をサポートしていません。

プロセス情報の取得に使用されるパフォーマンス カウンター API へのアクセスに関する問題があります。 この例外は、WINDOWS NT、Windows 2000、およびWindows XPに固有です。

基になるシステム API へのアクセスで問題が発生しました。

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

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 コンポーネントの配列を作成し、指定された (通常はリモート) コンピューター上のすべてのプロセス リソースに関連付けます。 GetProcessesはシステム リソースを作成せず、アプリケーションによって生成されたProcess コンポーネントにリソースを関連付けるので、プロセス リソースはローカル コンピューターに既に存在している必要があります。 オペレーティング システム自体はバックグラウンド プロセスを実行しているため、この配列は空ではありません。

コンピューター上で実行されているすべてのプロセスを取得しない場合は、 GetProcessById または GetProcessesByName メソッドを使用して、その数を制限できます。 GetProcessById は、メソッドに渡すプロセス識別子によってシステムで識別されるプロセスに関連付けられている Process コンポーネントを作成します。 GetProcessesByName は、関連付けられたプロセス リソースがメソッドに渡す実行可能ファイルを共有する Process コンポーネントの配列を作成します。

GetProcesses メソッドのこのオーバーロードは、通常、ネットワーク上のリモート コンピューターで実行されているプロセス リソースの一覧を取得するために使用されますが、"." を渡すことでローカル コンピューターを指定できます。

Note

サービス ホスト プロセス (svchost.exe) の同じインスタンス内に複数のWindows サービスを読み込むことができます。 GetProcesses はこれらの個々のサービスを識別しません。その場合は、 GetServicesを参照してください。

こちらもご覧ください

適用対象