[CODE]///获取任务管理器里的一些信息
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime .InteropServices ;
namespace MemoryInfo
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox MemoryLoad;
private System.Windows.Forms.TextBox TotalPageFile;
private System.Windows.Forms.TextBox TotalPhys;
private System.Windows.Forms.TextBox TotalVirtual;
private System.Windows.Forms.TextBox AvailPageFile;
private System.Windows.Forms.TextBox AvailPhys;
private System.Windows.Forms.TextBox AvailVirtual;
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.MemoryLoad = new System.Windows.Forms.TextBox();
this.TotalPageFile = new System.Windows.Forms.TextBox();
this.TotalPhys = new System.Windows.Forms.TextBox();
this.TotalVirtual = new System.Windows.Forms.TextBox();
this.AvailPageFile = new System.Windows.Forms.TextBox();
this.AvailPhys = new System.Windows.Forms.TextBox();
this.AvailVirtual = new System.Windows.Forms.TextBox();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(192, 192);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "刷新";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// MemoryLoad
//
this.MemoryLoad.Location = new System.Drawing.Point(8, 8);
this.MemoryLoad.Name = "MemoryLoad";
this.MemoryLoad.Size = new System.Drawing.Size(264, 21);
this.MemoryLoad.TabIndex = 1;
this.MemoryLoad.Text = "textBox1";
//
// TotalPageFile
//
this.TotalPageFile.Location = new System.Drawing.Point(8, 32);
this.TotalPageFile.Name = "TotalPageFile";
this.TotalPageFile.Size = new System.Drawing.Size(264, 21);
this.TotalPageFile.TabIndex = 2;
this.TotalPageFile.Text = "textBox1";
//
// TotalPhys
//
this.TotalPhys.Location = new System.Drawing.Point(8, 56);
this.TotalPhys.Name = "TotalPhys";
this.TotalPhys.Size = new System.Drawing.Size(264, 21);
this.TotalPhys.TabIndex = 3;
this.TotalPhys.Text = "textBox1";
//
// TotalVirtual
//
this.TotalVirtual.Location = new System.Drawing.Point(8, 80);
this.TotalVirtual.Name = "TotalVirtual";
this.TotalVirtual.Size = new System.Drawing.Size(264, 21);
this.TotalVirtual.TabIndex = 4;
this.TotalVirtual.Text = "textBox1";
//
// AvailPageFile
//
this.AvailPageFile.Location = new System.Drawing.Point(8, 104);
this.AvailPageFile.Name = "AvailPageFile";
this.AvailPageFile.Size = new System.Drawing.Size(264, 21);
this.AvailPageFile.TabIndex = 5;
this.AvailPageFile.Text = "textBox1";
//
// AvailPhys
//
this.AvailPhys.Location = new System.Drawing.Point(8, 128);
this.AvailPhys.Name = "AvailPhys";
this.AvailPhys.Size = new System.Drawing.Size(264, 21);
this.AvailPhys.TabIndex = 6;
this.AvailPhys.Text = "textBox1";
//
// AvailVirtual
//
this.AvailVirtual.Location = new System.Drawing.Point(8, 152);
this.AvailVirtual.Name = "AvailVirtual";
this.AvailVirtual.Size = new System.Drawing.Size(264, 21);
this.AvailVirtual.TabIndex = 7;
this.AvailVirtual.Text = "textBox1";
//
// timer1
//
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(280, 221);
this.Controls.Add(this.AvailVirtual);
this.Controls.Add(this.AvailPhys);
this.Controls.Add(this.AvailPageFile);
this.Controls.Add(this.TotalVirtual);
this.Controls.Add(this.TotalPhys);
this.Controls.Add(this.TotalPageFile);
this.Controls.Add(this.MemoryLoad);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
[DllImport("kernel32")]
public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
[StructLayout(LayoutKind.Sequential)]
public struct MEMORY_INFO
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
}
private void button1_Click(object sender, System.EventArgs e)
{
MEMORY_INFO MemInfo;
MemInfo = new MEMORY_INFO();
GlobalMemoryStatus(ref MemInfo);
MemoryLoad.Text = MemInfo.dwMemoryLoad.ToString()+"%的内存正在使用";
TotalPhys.Text = "物理内存共有"+MemInfo.dwTotalPhys.ToString()+"字节";
AvailPhys.Text = "可使用的物理内存有"+MemInfo.dwAvailPhys.ToString()+"字节";
TotalPageFile.Text = "交换文件总大小为"+MemInfo.dwTotalPageFile.ToString()+"字节";
AvailPageFile.Text = "尚可交换文件大小为"+MemInfo.dwAvailPageFile.ToString()+"字节";
TotalVirtual.Text = "总虚拟内存有"+MemInfo.dwTotalVirtual.ToString()+"字节";
AvailVirtual.Text = "未用虚拟内存有"+MemInfo.dwAvailVirtual.ToString()+"字节";
}
private void Form1_Load(object sender, System.EventArgs e)
{
timer1.Enabled =true;
}
private void timer1_Tick(object sender, System.EventArgs e)
{
timer1.Enabled =false;
MEMORY_INFO MemInfo;
MemInfo = new MEMORY_INFO();
GlobalMemoryStatus(ref MemInfo);
MemoryLoad.Text = MemInfo.dwMemoryLoad.ToString()+"%的内存正在使用";
TotalPhys.Text = "物理内存共有"+MemInfo.dwTotalPhys.ToString()+"字节";
AvailPhys.Text = "可使用的物理内存有"+MemInfo.dwAvailPhys.ToString()+"字节";
TotalPageFile.Text = "交换文件总大小为"+MemInfo.dwTotalPageFile.ToString()+"字节";
AvailPageFile.Text = "尚可交换文件大小为"+MemInfo.dwAvailPageFile.ToString()+"字节";
TotalVirtual.Text = "总虚拟内存有"+MemInfo.dwTotalVirtual.ToString()+"字节";
AvailVirtual.Text = "未用虚拟内存有"+MemInfo.dwAvailVirtual.ToString()+"字节";
timer1.Enabled =true;
}
}
}[/CODE]