前进路上,我们都曾经迷失!

使我停下来的,不是我所见,而是我所未见…… 你能体会吗?那些我看不清的东西,在那延绵不断的城市中,我能看到一切,可就是看不到尽头…… 什么都有,就是没有尽头……而尽头,正是我所未见。—— KimiYang

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

  本文将使用C#来获取系统的基本信息。

  结果如图所示:

  为了获取系统信息,我将调用windows自带的kernel32.dll程序。代码如下所示:

 

代码
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using System.Runtime.InteropServices;
 10 
 11 namespace getSystemInfo
 12 {
 13     
 14 
 15     public partial class Form1 : Form
 16     {
 17         public Form1()
 18         {
 19             InitializeComponent();
 20         }
 21 
 22         [DllImport("kernel32")]
 23         public static extern void GetWindowsDirectory(StringBuilder WinDir, int count);
 24 
 25         [DllImport("kernel32")]
 26         public static extern void GetSystemDirectory(StringBuilder SysDir, int count);
 27 
 28         [DllImport("kernel32")]
 29         public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);
 30 
 31         [DllImport("kernel32")]
 32         public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
 33 
 34         [DllImport("kernel32")]
 35         public static extern void GetSystemTime(ref SYSTEMTIME_INFO stinfo);
 36 
 37         //定义以下各结构
 38         //定义CPU的信息结构
 39         [StructLayout(LayoutKind.Sequential)]
 40         public struct CPU_INFO
 41         {
 42             public uint dwOemId;
 43             public uint dwPageSize;
 44             public uint lpMinimumApplicationAddress;
 45             public uint lpMaximumApplicationAddress;
 46             public uint dwActiveProcessorMask;
 47             public uint dwNumberOfProcessors;
 48             public uint dwProcessorType;
 49             public uint dwAllocationGranularity;
 50             public uint dwProcessorLevel;
 51             public uint dwProcessorRevision;
 52         }
 53 
 54         //定义内存的信息结构
 55         [StructLayout(LayoutKind.Sequential)]
 56         public struct MEMORY_INFO
 57         {
 58             public uint dwLength;
 59             public uint dwMemoryLoad;
 60             public uint dwTotalPhys;
 61             public uint dwAvailPhys;
 62             public uint dwTotalPageFile;
 63             public uint dwAvailPageFile;
 64             public uint dwTotalVirtual;
 65             public uint dwAvailVirtual;
 66         }
 67 
 68         //定义系统时间的信息结构
 69         [StructLayout(LayoutKind.Sequential)]
 70         public struct SYSTEMTIME_INFO
 71         {
 72             public ushort wYear;
 73             public ushort wMonth;
 74             public ushort wDayOfWeek;
 75             public ushort wDay;
 76             public ushort wHour;
 77             public ushort wMinute;
 78             public ushort wSecond;
 79             public ushort wMilliseconds;
 80         }
 81 
 82         private void button1_Click(object sender, EventArgs e)
 83         {
 84             string strResult = null;
 85             //调用GetWindowsDirectory和GetSystemDirectory函数分别取得Windows路径和系统路径
 86             const int nChars = 128;
 87             StringBuilder Buff = new StringBuilder(nChars);
 88             GetWindowsDirectory(Buff, nChars);
 89             labWindowPath.Text = "Windows路径:" + Buff.ToString();
 90             GetSystemDirectory(Buff, nChars);
 91             lbSystemPath.Text = "系统路径:" + Buff.ToString();
 92 
 93             //调用GetSystemInfo函数获取CPU的相关信息
 94             CPU_INFO CpuInfo;
 95             CpuInfo = new CPU_INFO();
 96             GetSystemInfo(ref CpuInfo);
 97             lbCupCount.Text = "本计算机中有" + CpuInfo.dwNumberOfProcessors.ToString() + "个CPU";
 98             lbCpuType.Text = "CPU的类型为" + CpuInfo.dwProcessorType.ToString();
 99             strResult  += "CPU等级为" + CpuInfo.dwProcessorLevel.ToString();
100             lbCpuEom.Text = "CPU的OEM ID为" + CpuInfo.dwOemId.ToString();
101             strResult  += "CPU中的页面大小为" + CpuInfo.dwPageSize.ToString();
102 
103             //调用GlobalMemoryStatus函数获取内存的相关信息
104             MEMORY_INFO MemInfo;
105             MemInfo = new MEMORY_INFO();
106             GlobalMemoryStatus(ref MemInfo);
107             lbNcsyl.Text = MemInfo.dwMemoryLoad.ToString() + "%的内存正在使用";
108             LbNCTotal.Text = "物理内存共有" + MemInfo.dwTotalPhys.ToString() + "字节";
109             LbNcKy.Text = "可使用的物理内存有" + MemInfo.dwAvailPhys.ToString() + "字节";
110             strResult  += "交换文件总大小为" + MemInfo.dwTotalPageFile.ToString() + "字节";
111             strResult  += "尚可交换文件大小为" + MemInfo.dwAvailPageFile.ToString() + "字节";
112             LbXnncTotal.Text = "总虚拟内存有" + MemInfo.dwTotalVirtual.ToString() + "字节";
113             LbKyxnnc.Text = "未用虚拟内存有" + MemInfo.dwAvailVirtual.ToString() + "字节";
114 
115             //调用GetSystemTime函数获取系统时间信息
116             SYSTEMTIME_INFO StInfo;
117             StInfo = new SYSTEMTIME_INFO();
118             GetSystemTime(ref StInfo);
119             LabRq.Text = StInfo.wYear.ToString() + "" + StInfo.wMonth.ToString() + "" + StInfo.wDay.ToString() + "";
120             lbXtrq.Text = (StInfo.wHour + 8).ToString() + "" + StInfo.wMinute.ToString() + "" + StInfo.wSecond.ToString() + "";
121            // label1.Text = strResult.ToString();
122 
123             //获取计算机名称
124             lbCompName.Text += System.Environment.UserName;
125             lbMaName.Text += System.Environment.MachineName;
126             lbOSInfo.Text += System.Environment.OSVersion;
127             lbNetName.Text += System.Environment.UserDomainName;
128         }
129     }
130 }
131 

 

 

posted on 2010-04-08 20:28  KimiYang  阅读(438)  评论(0编辑  收藏  举报