PerformanceCounterを使ってNetwork帯域使用量を取得する

PerformanceCounterを使ってNetwork帯域使用量を取得する例です。

FormにLabelとTimerコントロールを配置し
100ミリ秒毎に表示しています。

InstanceNameにネットワークデバイス名を設定する点に注意です。
デバイス名はDOSコマンドのipconfig/allなどで取得できますが
プログラムでこれを取得する方法は追々別節で説明する予定です。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        System.Diagnostics.PerformanceCounter pc = new System.Diagnostics.PerformanceCounter();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pc.CategoryName = "Network Interface";
            pc.CounterName = "Bytes Total/sec";
            pc.InstanceName = "NVIDIA nForce Networking Controller";

            // 100ミリ秒間隔でタイマー設定
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Interval = 100;
            timer1.Enabled = true;
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = pc.NextValue().ToString() + "Byte Total/sec";
        }
    }
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)