运用API函数获取系统信息
运用API函数获取系统信息 using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Text;
namespace WindowsApplication10
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.GroupBox groupBox4;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label AvailVirtual;
private System.Windows.Forms.Label TotalVirtual;
private System.Windows.Forms.Label AvailPageFile;
private System.Windows.Forms.Label TotalPageFile;
private System.Windows.Forms.Label AvailPhys;
private System.Windows.Forms.Label TotalPhys;
private System.Windows.Forms.Label MemoryLoad;
private System.Windows.Forms.Label PageSize;
private System.Windows.Forms.Label OemId;
private System.Windows.Forms.Label ProcessorLevel;
private System.Windows.Forms.Label ProcessorType;
private System.Windows.Forms.Label NumberOfProcessors;
private System.Windows.Forms.Label SystemDirectory;
private System.Windows.Forms.Label WindowsDirectory;
private System.Windows.Forms.Label Time;
private System.Windows.Forms.Label Date;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
[DllImport('kernel32')]
public static extern void GetWindowsDirectory(StringBuilder WinDir,int count);
[DllImport('kernel32')]
public static extern void GetSystemDirectory(StringBuilder SysDir,int count);
[DllImport('kernel32')]
public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);
[DllImport('kernel32')]
public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
[DllImport('kernel32')]
public static extern void GetSystemTime(ref SYSTEMTIME_INFO stinfo);
[StructLayout(LayoutKind.Sequential)]
public struct CPU_INFO
{
public uint dwOemId;
public uint dwPageSize;
public uint lpMinimumApplicationAddress;
public uint lpMaximumApplicationAddress;
public uint dwActiveProcessorMask;
public uint dwNumberOfProcessors;
public uint dwProcessorType;
public uint dwAllocationGranularity;
public uint dwProcessorLevel;
public uint dwProcessorRevision;
}
//定义内存的信息结构
[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;
}
//定义系统时间的信息结构
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME_INFO
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}
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.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.button1 = new System.Windows.Forms.Button();
this.Time = new System.Windows.Forms.Label();
this.Date = new System.Windows.Forms.Label();
this.TotalPhys = new System.Windows.Forms.Label();
this.MemoryLoad = new System.Windows.Forms.Label();
this.AvailPageFile = new System.Windows.Forms.Label();
this.TotalVirtual = new System.Windows.Forms.Label();
this.AvailPhys = new System.Windows.Forms.Label();
this.TotalPageFile = new System.Windows.Forms.Label();
this.PageSize = new System.Windows.Forms.Label();
this.OemId = new System.Windows.Forms.Label();
this.AvailVirtual = new System.Windows.Forms.Label();
this.ProcessorLevel = new System.Windows.Forms.Label();
this.NumberOfProcessors = new System.Windows.Forms.Label();
this.ProcessorType = new System.Windows.Forms.Label();
this.WindowsDirectory = new System.Windows.Forms.Label();
this.SystemDirectory = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.groupBox4.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.Date);
this.groupBox1.Controls.Add(this.Time);
this.groupBox1.Location = new System.Drawing.Point(8, 16);
this.groupBox1.Name = 'groupBox1';
this.groupBox1.Size = new System.Drawing.Size(240, 128);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = 'groupBox1';
//
// groupBox2
//
this.groupBox2.Controls.Add(this.TotalVirtual);
this.groupBox2.Controls.Add(this.AvailVirtual);
this.groupBox2.Location = new System.Drawing.Point(296, 24);
this.groupBox2.Name = 'groupBox2';
this.groupBox2.Size = new System.Drawing.Size(240, 120);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = 'groupBox2';
//
// groupBox3
//
this.groupBox3.Controls.Add(this.ProcessorType);
this.groupBox3.Controls.Add(this.NumberOfProcessors);
this.groupBox3.Controls.Add(this.MemoryLoad);
this.groupBox3.Controls.Add(this.PageSize);
this.groupBox3.Controls.Add(this.OemId);
this.groupBox3.Controls.Add(this.ProcessorLevel);
this.groupBox3.Location = new System.Drawing.Point(296, 160);
this.groupBox3.Name = 'groupBox3';
this.groupBox3.Size = new System.Drawing.Size(240, 256);
this.groupBox3.TabIndex = 1;
this.groupBox3.TabStop = false;
this.groupBox3.Text = 'groupBox2';
//
// groupBox4
//
this.groupBox4.Controls.Add(this.AvailPageFile);
this.groupBox4.Controls.Add(this.TotalPageFile);
this.groupBox4.Controls.Add(this.AvailPhys);
this.groupBox4.Controls.Add(this.TotalPhys);
this.groupBox4.Controls.Add(this.WindowsDirectory);
this.groupBox4.Location = new System.Drawing.Point(16, 160);
this.groupBox4.Name = 'groupBox4';
this.groupBox4.Size = new System.Drawing.Size(240, 296);
this.groupBox4.TabIndex = 2;
this.groupBox4.TabStop = false;
this.groupBox4.Text = 'groupBox2';
//
// button1
//
this.button1.Location = new System.Drawing.Point(248, 456);
this.button1.Name = 'button1';
this.button1.TabIndex = 3;
this.button1.Text = 'button1';
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Time
//
this.Time.Location = new System.Drawing.Point(32, 48);
this.Time.Name = 'Time';
this.Time.Size = new System.Drawing.Size(160, 23);
this.Time.TabIndex = 0;
this.Time.Text = 'label1';
//
// Date
//
this.Date.Location = new System.Drawing.Point(32, 96);
this.Date.Name = 'Date';
this.Date.Size = new System.Drawing.Size(160, 23);
this.Date.TabIndex = 1;
this.Date.Text = 'label2';
//
// TotalPhys
//
this.TotalPhys.Location = new System.Drawing.Point(32, 208);
this.TotalPhys.Name = 'TotalPhys';
this.TotalPhys.Size = new System.Drawing.Size(160, 23);
this.TotalPhys.TabIndex = 2;
this.TotalPhys.Text = 'label3';
//
// MemoryLoad
//
this.MemoryLoad.Location = new System.Drawing.Point(40, 56);
this.MemoryLoad.Name = 'MemoryLoad';
this.MemoryLoad.Size = new System.Drawing.Size(168, 23);
this.MemoryLoad.TabIndex = 3;
this.MemoryLoad.Text = 'label4';
//
// AvailPageFile
//
this.AvailPageFile.Location = new System.Drawing.Point(32, 64);
this.AvailPageFile.Name = 'AvailPageFile';
this.AvailPageFile.Size = new System.Drawing.Size(160, 23);
this.AvailPageFile.TabIndex = 4;
this.AvailPageFile.Text = 'label5';
//
// TotalVirtual
//
this.TotalVirtual.Location = new System.Drawing.Point(40, 40);
this.TotalVirtual.Name = 'TotalVirtual';
this.TotalVirtual.Size = new System.Drawing.Size(168, 23);
this.TotalVirtual.TabIndex = 5;
this.TotalVirtual.Text = 'label6';
//
// AvailPhys
//
this.AvailPhys.Location = new System.Drawing.Point(32, 160);
this.AvailPhys.Name = 'AvailPhys';
this.AvailPhys.Size = new System.Drawing.Size(160, 23);
this.AvailPhys.TabIndex = 6;
this.AvailPhys.Text = 'label7';
//
// TotalPageFile
//
this.TotalPageFile.Location = new System.Drawing.Point(32, 112);
this.TotalPageFile.Name = 'TotalPageFile';
this.TotalPageFile.Size = new System.Drawing.Size(160, 23);
this.TotalPageFile.TabIndex = 7;
this.TotalPageFile.Text = 'label8';
//
// PageSize
//
this.PageSize.Location = new System.Drawing.Point(40, 96);
this.PageSize.Name = 'PageSize';
this.PageSize.Size = new System.Drawing.Size(168, 23);
this.PageSize.TabIndex = 8;
this.PageSize.Text = 'label9';
//
// OemId
//
this.OemId.Location = new System.Drawing.Point(40, 144);
this.OemId.Name = 'OemId';
this.OemId.Size = new System.Drawing.Size(168, 23);
this.OemId.TabIndex = 9;
this.OemId.Text = 'label10';
//
// AvailVirtual
//
this.AvailVirtual.Location = new System.Drawing.Point(40, 80);
this.AvailVirtual.Name = 'AvailVirtual';
this.AvailVirtual.Size = new System.Drawing.Size(168, 23);
this.AvailVirtual.TabIndex = 10;
this.AvailVirtual.Text = 'label11';
//
// ProcessorLevel
//
this.ProcessorLevel.Location = new System.Drawing.Point(40, 184);
this.ProcessorLevel.Name = 'ProcessorLevel';
this.ProcessorLevel.Size = new System.Drawing.Size(168, 23);
this.ProcessorLevel.TabIndex = 11;
this.ProcessorLevel.Text = 'label12';
//
// NumberOfProcessors
//
this.NumberOfProcessors.Location = new System.Drawing.Point(40, 24);
this.NumberOfProcessors.Name = 'NumberOfProcessors';
this.NumberOfProcessors.Size = new System.Drawing.Size(168, 23);
this.NumberOfProcessors.TabIndex = 12;
this.NumberOfProcessors.Text = 'label1';
//
// ProcessorType
//
this.ProcessorType.Location = new System.Drawing.Point(40, 216);
this.ProcessorType.Name = 'ProcessorType';
this.ProcessorType.Size = new System.Drawing.Size(168, 23);
this.ProcessorType.TabIndex = 13;
this.ProcessorType.Text = 'label2';
//
// WindowsDirectory
//
this.WindowsDirectory.Location = new System.Drawing.Point(32, 24);
this.WindowsDirectory.Name = 'WindowsDirectory';
this.WindowsDirectory.Size = new System.Drawing.Size(160, 23);
this.WindowsDirectory.TabIndex = 11;
this.WindowsDirectory.Text = 'label1';
//
// SystemDirectory
//
this.SystemDirectory.Location = new System.Drawing.Point(32, 400);
this.SystemDirectory.Name = 'SystemDirectory';
this.SystemDirectory.TabIndex = 2;
this.SystemDirectory.Text = 'label2';
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(568, 507);
this.Controls.Add(this.button1);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox4);
this.Controls.Add(this.SystemDirectory);
this.Name = 'Form1';
this.Text = 'Form1';
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
this.groupBox4.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
//调用GetWindowsDirectory和GetSystemDirectory函数分别取得Windows路径和系统路径
const int nChars = 128;
StringBuilder Buff = new StringBuilder(nChars);
GetWindowsDirectory(Buff,nChars);
WindowsDirectory.Text = 'Windows路径:'+Buff.ToString();
GetSystemDirectory(Buff,nChars);
SystemDirectory.Text = '系统路径:'+Buff.ToString();
//调用GetSystemInfo函数获取CPU的相关信息
CPU_INFO CpuInfo;
CpuInfo = new CPU_INFO();
GetSystemInfo(ref CpuInfo);
NumberOfProcessors.Text = '本计算机中有'+CpuInfo.dwNumberOfProcessors.ToString()+'个CPU';
ProcessorType.Text = 'CPU的类型为'+CpuInfo.dwProcessorType.ToString();
ProcessorLevel.Text = 'CPU等级为'+CpuInfo.dwProcessorLevel.ToString();
OemId.Text = 'CPU的OEM ID为'+CpuInfo.dwOemId.ToString();
PageSize.Text = 'CPU中的页面大小为'+CpuInfo.dwPageSize.ToString();
//调用GlobalMemoryStatus函数获取内存的相关信息
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()+'字节';
//调用GetSystemTime函数获取系统时间信息
SYSTEMTIME_INFO StInfo;
StInfo = new SYSTEMTIME_INFO();
GetSystemTime(ref StInfo);
Date.Text = StInfo.wYear.ToString()+'年'+StInfo.wMonth.ToString()+'月'+StInfo.wDay.ToString()+'日';
Time.Text = (StInfo.wHour+8).ToString()+'点'+StInfo.wMinute.ToString()+'分'+StInfo.wSecond.ToString()+'秒';
}
}
}