[ VB.NET / C# ] 実行中プロセスの各種情報を取得 ( Process.GetCurrentProcess )

Pocket

ここでは、VB.NET および C# で実行中プロセスのメモリ使用量などの各種プロセス情報を取得するサンプルコードを掲載しています。

スポンサーリンク

プロセス情報を取得する

Process.GetCurrentProcess で実行中のプロセス情報を取得するサンプルコードです。詳細はサンプル内のコメントや下段の参考リンクを参照ください。

VB.NET

    Private Sub Form1_Load(ByVal sender As Object, _
                           ByVal e As System.EventArgs) Handles Me.Load

        ' 自プロセスを取得
        Dim currentProcess As System.Diagnostics.Process = System.Diagnostics.Process.GetCurrentProcess()

        ' リフレッシュしないとプロセスの各種情報が最新情報に更新されない
        currentProcess.Refresh()

        ' 各種プロセス情報を出力する
        Console.WriteLine("プロセスID      : {0}", currentProcess.Id)
        Console.WriteLine("プロセス名      : {0}", currentProcess.ProcessName)
        Console.WriteLine("基本優先度      : {0}", currentProcess.BasePriority)

        Console.WriteLine("物理メモリ使用量: {0}", currentProcess.WorkingSet64)
        Console.WriteLine("仮想メモリ使用量: {0}", currentProcess.VirtualMemorySize64)

        ' 出力結果例
        '------------------------------------------
        ' プロセスID      : 5268
        ' プロセス名      : WindowsApplication1
        ' 基本優先度      : 8
        ' 物理メモリ使用量: 28246016
        ' 仮想メモリ使用量: 211927040

        ' その他様々なプロパティが用意されている

    End Sub

C#

    private void Form1_Load(object sender, EventArgs e)
    {
        // 自プロセスを取得
        System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();

        // リフレッシュしないとプロセスの各種情報が最新情報に更新されない
        currentProcess.Refresh();

        // 各種プロセス情報を出力する
        Console.WriteLine("プロセスID      : {0}", currentProcess.Id);
        Console.WriteLine("プロセス名      : {0}", currentProcess.ProcessName);
        Console.WriteLine("基本優先度      : {0}", currentProcess.BasePriority);

        Console.WriteLine("物理メモリ使用量: {0}", currentProcess.WorkingSet64);
        Console.WriteLine("仮想メモリ使用量: {0}", currentProcess.VirtualMemorySize64);

        // 出力結果例
        //------------------------------------------
        // プロセスID      : 5268
        // プロセス名      : WindowsFormsApplication1
        // 基本優先度      : 8
        // 物理メモリ使用量: 28246016
        // 仮想メモリ使用量: 211927040

        /*
         * その他、様々なプロパティが用意されている
         */
    }
参考
スポンサーリンク


Pocket

Leave a Comment

Your email address will not be published. Required fields are marked *