CodePanic! > C#.NET Tips > 今ここ
■PerformanceCounterを使って空きメモリ情報を取得する
PerformanceCounterを使って空きメモリ情報を取得する例です。
FormにLabelとTimerコントロールを配置し
100ミリ秒毎に空きメモリ情報を表示しています。
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 = "memory";
// 物理空き容量を取得
pc.CounterName = "Available MBytes";
// 対象はローカルPC
pc.MachineName = ".";
// 100ミリ秒間隔でタイマー設定
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 100;
timer1.Enabled = true;
}
void timer1_Tick(object sender, EventArgs e)
{
label1.Text = pc.NextValue() + "MB";
}
}
}
Copyright © 2008.07 - shougo suzaki
|