C#程序设计获取系统信息的Windows窗体浅析

开发 后端
C#程序设计获取系统信息的Windows窗体主要向你介绍了使用从C#实现关机程序时的界面相关,那么具体的是什麽过程呢?本文就向你介绍相关的内容。

C#程序设计获取系统信息的Windows窗体的背景许多软件都有自动关机功能,特别是在长时间下载的时候,这个功能可是使你不用以守候在计算机前面,而电脑却能按照您事先的设定自动关闭。现在我们用visual C#来编写一个多功能的关机程序。C#程序设计获取系统信息的Windows窗体是该程序的一部分,现在让我们来看看具体的过程。

C#程序设计获取系统信息的Windows窗体实现的步骤:

C#程序设计获取系统信息的Windows窗体实现1. 界面的设计

向工程中增加一个Windows窗体并向窗体中添加如下控件:

向窗体中添加控件 

C#程序设计获取系统信息的Windows窗体实现2. 在窗体类中引用API函数

  1. using System.Runtime.InteropServices ;   
  2. using System.Text ;   
  3. [ DllImport("kernel32") ]   
  4. public static extern void GetWindowsDirectory(StringBuilder WinDir,int count) ;   
  5. [ DllImport("kernel32") ]   
  6. public static extern void GetSystemDirectory(StringBuilder SysDir,int count) ;   
  7. [ DllImport("kernel32") ]   
  8. public static extern void GetSystemInfo(ref CPU_INFO cpuinfo) ;   
  9. [ DllImport("kernel32") ]   
  10. public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo) ;   
  11. [ DllImport("kernel32") ]   
  12. public static extern void GetSystemTime(ref SYSTEMTIME_INFO stinfo) ;  

以上几个API的作用分别是获取系统路径,获得CPU相关信息,获得内存的相关信息,获得系统时间等。

C#程序设计获取系统信息的Windows窗体实现3. 定义以下各结构

在声明完所有的API函数后,我们发现后三个函数分别用到了CPU_INFO、MEMORY_INFO、SYSTEMTIME_INFO等结构,这些结构并非是.Net内部的,它们从何而来?其实,我们在用到以上API调用时均需用到以上结构,我们将函数调用获得的信息存放在以上的结构体中,***返回给程序输出。这些结构体比较复杂,但是如果开发者能够熟练运用,那么整个API世界将尽在开发者的掌握之中。以下就是上述结构体的声明:

  1. //定义CPU的信息结构   
  2. [StructLayout(LayoutKind.Sequential) ]   
  3. public struct CPU_INFO   
  4. {   
  5. public uint dwOemId ;   
  6. public uint dwPageSize ;   
  7. public uint lpMinimumApplicationAddress ;   
  8. public uint lpMaximumApplicationAddress ;   
  9. public uint dwActiveProcessorMask ;   
  10. public uint dwNumberOfProcessors ;   
  11. public uint dwProcessorType ;   
  12. public uint dwAllocationGranularity ;   
  13. public uint dwProcessorLevel ;   
  14. public uint dwProcessorRevision ;   
  15. }   
  16.  
  17. file://定义内存的信息结构   
  18. [StructLayout(LayoutKind.Sequential) ]   
  19. public struct MEMORY_INFO   
  20. {   
  21. public uint dwLength ;   
  22. public uint dwMemoryLoad ;   
  23. public uint dwTotalPhys ;   
  24. public uint dwAvailPhys ;   
  25. public uint dwTotalPageFile ;   
  26. public uint dwAvailPageFile ;   
  27. public uint dwTotalVirtual ;   
  28. public uint dwAvailVirtual ;   
  29. }   
  30.  
  31. file://定义系统时间的信息结构   
  32. [StructLayout(LayoutKind.Sequential) ]   
  33. public struct SYSTEMTIME_INFO   
  34. {   
  35. public ushort wYear ;   
  36. public ushort wMonth ;   
  37. public ushort wDayOfWeek ;   
  38. public ushort wDay ;   
  39. public ushort wHour ;   
  40. public ushort wMinute ;   
  41. public ushort wSecond ;   
  42. public ushort wMilliseconds ;   
  43. }   

C#程序设计获取系统信息的Windows窗体实现4. 编写窗体类的方法

  1. private void button1_Click(object sender, System.EventArgs e )   
  2. {   
  3. file://调用GetWindowsDirectory和GetSystemDirectory函数分别取得Windows路径和系统路径   
  4. const int nChars = 128 ;   
  5. StringBuilder Buff = new StringBuilder(nChars) ;   
  6. GetWindowsDirectory(Buff,nChars) ;   
  7. WindowsDirectory.Text = "Windows路径:"+Buff.ToString( ) ;   
  8. GetSystemDirectory(Buff,nChars) ;   
  9. SystemDirectory.Text = " 系统路径:"+Buff.ToString( ) ;   
  10.  
  11. file://调用GetSystemInfo函数获取CPU的相关信息   
  12. CPU_INFO CpuInfo ;   
  13. CpuInfo = new CPU_INFO( ) ;   
  14. GetSystemInfo(ref CpuInfo) ;   
  15. NumberOfProcessors.Text =   
  16. "本计算机中有"+CpuInfo.dwNumberOfProcessors.ToString( ) +"个CPU";   
  17. ProcessorType.Text = "CPU的类型为"+CpuInfo.dwProcessorType.ToString( ) ;   
  18. ProcessorLevel.Text = "CPU等级为"+CpuInfo.dwProcessorLevel.ToString( ) ;   
  19. OemId.Text = "CPU的OEM ID为"+CpuInfo.dwOemId.ToString( ) ;   
  20. PageSize.Text = "CPU中的页面大小为"+CpuInfo.dwPageSize.ToString( ) ;   
  21.  
  22. file://调用GlobalMemoryStatus函数获取内存的相关信息   
  23. MEMORY_INFO MemInfo ;   
  24. MemInfo = new MEMORY_INFO( ) ;   
  25. GlobalMemoryStatus(ref MemInfo) ;   
  26. MemoryLoad.Text =   
  27. MemInfo.dwMemoryLoad.ToString( ) +"%的内存正在使用" ;   
  28. TotalPhys.Text =   
  29. "物理内存共有"+MemInfo.dwTotalPhys.ToString( ) +"字节" ;   
  30. AvailPhys.Text =   
  31. "可使用的物理内存有"+MemInfo.dwAvailPhys.ToString( ) +"字节" ;   
  32. TotalPageFile.Text =   
  33. "交换文件总大小为"+MemInfo.dwTotalPageFile.ToString( ) +"字节" ;   
  34. AvailPageFile.Text =   
  35. "尚可交换文件大小为"+MemInfo.dwAvailPageFile.ToString( ) +"字节" ;   
  36. TotalVirtual.Text =   
  37. "总虚拟内存有"+MemInfo.dwTotalVirtual.ToString( ) +"字节" ;   
  38. AvailVirtual.Text =   
  39. "未用虚拟内存有"+MemInfo.dwAvailVirtual.ToString( ) +"字节" ;   
  40.  
  41. file://调用GetSystemTime函数获取系统时间信息   
  42. SYSTEMTIME_INFO StInfo ;   
  43. StInfo = new SYSTEMTIME_INFO( ) ;   
  44. GetSystemTime(ref StInfo) ;   
  45. Date.Text = StInfo.wYear.ToString( ) +  
  46. "年"+StInfo.wMonth.ToString( ) +"月"+  
  47. StInfo.wDay.ToString( ) +"日" ;   
  48. Time.Text = (StInfo.wHour+8).ToString( ) +  
  49. "点"+StInfo.wMinute.ToString( ) +"分"+  
  50. StInfo.wSecond.ToString( ) +"秒" ;   
  51. }   

C#程序设计获取系统信息的Windows窗体实现的基本情况就向你介绍到这里,希望对你了解和学习C#程序设计获取系统信息的Windows窗体实现有所帮助。

【编辑推荐】

  1. 如何初始化数组详解
  2. C#数组操作的体会浅谈
  3. C#关机代码实例详解
  4. C#关机代码的实现浅析
  5. C#程序设计关闭Windows窗体浅析
责任编辑:仲衡 来源: it.21cn.com
相关推荐

2009-09-02 17:28:26

C#程序设计Windows窗体

2009-08-14 16:41:22

C#启动Windows

2009-08-25 09:39:21

创建C# Window

2009-09-07 06:07:46

C#窗体设计

2009-08-20 10:10:55

C#透明窗体

2009-08-21 17:48:43

C#网络编程

2010-01-11 10:34:22

C++程序

2009-09-07 04:19:56

C#窗体事件

2009-08-14 11:00:16

C#创建Windows

2009-08-21 17:33:34

服务器端程序C#网络编程

2009-09-07 04:56:52

C#模式窗体

2009-09-07 06:56:46

C#透明窗体

2009-09-02 13:22:23

C#组件化程序设计

2009-09-07 03:37:51

C#窗体

2009-09-07 05:31:39

C#窗体关闭事件

2009-09-07 05:24:22

C#窗体继承

2012-03-14 10:48:05

C#

2009-08-14 16:02:50

C#启动windows

2010-01-11 17:22:02

2009-09-07 14:00:57

C#抓取网页
点赞
收藏

51CTO技术栈公众号