分享
 
 
 

运用API函数获取系统信息

王朝c#·作者佚名  2006-12-17
窄屏简体版  字體: |||超大  

运用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()+'秒';

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有