首先要添加“引用”一个dll,选择“System Management”;
再引入2个命名空间:
using System.Management;
using System.IO;
foreach循环:声明一个迭代变量自动获取数组中每个元素的值。
String.Format:格式化字符,本站就有解释。


1
Form1.cs
2
3
using System;
4
using System.Collections.Generic;
5
using System.ComponentModel;
6
using System.Data;
7
using System.Drawing;
8
using System.Text;
9
using System.Windows.Forms;
10
using System.Management;
11
using System.IO;
12
namespace WindowsApplication1
13

{
14
public partial class Form1 : Form
15
{
16
public Form1()
17
{
18
InitializeComponent();
19
}
20
21
private void button1_Click(object sender, EventArgs e)
22
{
23
//获取CPU编号
24
ManagementClass MyClass = new ManagementClass("Win32_Processor");
25
ManagementObjectCollection MyCollection = MyClass.GetInstances();
26
String MyInfo = "当前系统CPU编号是:";
27
string MyCPUID = "";
28
foreach (ManagementObject MyObject in MyCollection)
29
{
30
MyCPUID = MyObject.Properties["ProcessorId"].Value.ToString();
31
break;
32
}
33
MyInfo += MyCPUID;
34
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
35
}
36
37
private void button2_Click(object sender, EventArgs e)
38
{
39
//获取计算机CPU的当前电压
40
String MyInfo = "计算机CPU的当前电压是:";
41
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
42
foreach (ManagementObject MyObject in MySearcher.Get())
43
{
44
try
45
{
46
MyInfo += "\n" + String.Format("CurrentVoltage : " + MyObject["CurrentVoltage"].ToString());
47
MyInfo += "\n=========================================================";
48
}
49
catch
{ }
50
}
51
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
52
}
53
54
private void button3_Click(object sender, EventArgs e)
55
{
56
//获取计算机CPU的外部频率
57
String MyInfo = "计算机CPU的外部频率是:";
58
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
59
foreach (ManagementObject MyObject in MySearcher.Get())
60
{
61
try
62
{
63
MyInfo += "\n" + String.Format("ExtClock : " + MyObject["ExtClock"].ToString());
64
MyInfo += "\n=========================================================";
65
}
66
catch
{ }
67
}
68
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
69
}
70
71
private void button4_Click(object sender, EventArgs e)
72
{
73
//获取计算机CPU的二级缓存
74
String MyInfo = "计算机CPU的二级缓存尺寸是:";
75
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
76
foreach (ManagementObject MyObject in MySearcher.Get())
77
{
78
MyInfo += "\n" + String.Format("L2CacheSize: " + MyObject["L2CacheSize"].ToString());
79
MyInfo += "\n=========================================================";
80
}
81
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
82
}
83
84
private void button5_Click(object sender, EventArgs e)
85
{
86
//获取计算机CPU的制造商名称
87
String MyInfo = "计算机CPU的制造商名称是:";
88
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
89
foreach (ManagementObject MyObject in MySearcher.Get())
90
{
91
MyInfo += "\n" + String.Format("Manufacturer : " + MyObject["Manufacturer"].ToString());
92
MyInfo += "\n=========================================================";
93
}
94
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
95
}
96
97
private void button6_Click(object sender, EventArgs e)
98
{
99
//获取计算机CPU的产品名称
100
String MyInfo = "计算机CPU的产品名称是:";
101
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
102
foreach (ManagementObject MyObject in MySearcher.Get())
103
{
104
MyInfo += "\n" + String.Format("Name : " + MyObject["Name"].ToString());
105
MyInfo += "\n=========================================================";
106
}
107
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
108
109
}
110
111
private void button7_Click(object sender, EventArgs e)
112
{
113
//获取计算机CPU的版本信息
114
String MyInfo = "计算机CPU的版本信息如下:";
115
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
116
foreach (ManagementObject MyObject in MySearcher.Get())
117
{
118
MyInfo += "\n" + String.Format("Version: " + MyObject["Version"].ToString());
119
MyInfo += "\n=========================================================";
120
}
121
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
122
123
}
124
125
private void button8_Click(object sender, EventArgs e)
126
{
127
//获取计算机CPU的当前使用百分比 注意要把SQLserver或者其他耗CPU的软件开着否则看不到效果就一直为0
128
String MyInfo = "计算机CPU的当前使用百分比是:";
129
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
130
foreach (ManagementObject MyObject in MySearcher.Get())
131
{
132
MyInfo += "\n" + String.Format("LoadPercentage : " + MyObject["LoadPercentage"].ToString());
133
MyInfo += "\n=========================================================";
134
}
135
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
136
137
}
138
139
private void button9_Click(object sender, EventArgs e)
140
{
141
//获取计算机CPU的最大时钟频率
142
String MyInfo = "计算机CPU的最大时钟频率是:";
143
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
144
foreach (ManagementObject MyObject in MySearcher.Get())
145
{
146
MyInfo += "\n" + String.Format("MaxClockSpeed : " + MyObject["MaxClockSpeed"].ToString());
147
MyInfo += "\n=========================================================";
148
}
149
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
150
151
}
152
153
private void button10_Click(object sender, EventArgs e)
154
{
155
//获取计算机CPU的当前时钟频率
156
String MyInfo = "计算机CPU的当前时钟频率是:";
157
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
158
foreach (ManagementObject MyObject in MySearcher.Get())
159
{
160
MyInfo += "\n" + String.Format("CurrentClockSpeed : " + MyObject["CurrentClockSpeed"].ToString());
161
MyInfo += "\n=========================================================";
162
}
163
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
164
165
}
166
167
private void button11_Click(object sender, EventArgs e)
168
{
169
//获取计算机的CPU地址宽度
170
String MyInfo = "当前计算机的CPU地址宽度是:";
171
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
172
foreach (ManagementObject MyObject in MySearcher.Get())
173
{
174
MyInfo += "\n" + String.Format("AddressWidth: " + MyObject["AddressWidth"].ToString());
175
MyInfo += "\n=========================================================";
176
}
177
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
178
179
}
180
181
private void button14_Click(object sender, EventArgs e)
182
{
183
//获取计算机的CPU数据宽度
184
String MyInfo = "当前计算机的CPU数据宽度是:";
185
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
186
foreach (ManagementObject MyObject in MySearcher.Get())
187
{
188
MyInfo += "\n" + String.Format("DataWidth : " + MyObject["DataWidth"].ToString());
189
MyInfo += "\n=========================================================";
190
}
191
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
192
193
}
194
}
195
}
196
197
Form1.Designer.cs
198
199
namespace WindowsApplication1
200

{
201
partial class Form1
202
{
203
/**//// <summary>
204
/// 必需的设计器变量。
205
/// </summary>
206
private System.ComponentModel.IContainer components = null;
207
208
/**//// <summary>
209
/// 清理所有正在使用的资源。
210
/// </summary>
211
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
212
protected override void Dispose(bool disposing)
213
{
214
if (disposing && (components != null))
215
{
216
components.Dispose();
217
}
218
base.Dispose(disposing);
219
}
220
221
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
222
223
/**//// <summary>
224
/// 设计器支持所需的方法 - 不要
225
/// 使用代码编辑器修改此方法的内容。
226
/// </summary>
227
private void InitializeComponent()
228
{
229
this.button1 = new System.Windows.Forms.Button();
230
this.button2 = new System.Windows.Forms.Button();
231
this.button3 = new System.Windows.Forms.Button();
232
this.button4 = new System.Windows.Forms.Button();
233
this.button5 = new System.Windows.Forms.Button();
234
this.button6 = new System.Windows.Forms.Button();
235
this.button7 = new System.Windows.Forms.Button();
236
this.button8 = new System.Windows.Forms.Button();
237
this.button9 = new System.Windows.Forms.Button();
238
this.button10 = new System.Windows.Forms.Button();
239
this.button11 = new System.Windows.Forms.Button();
240
this.button14 = new System.Windows.Forms.Button();
241
this.label1 = new System.Windows.Forms.Label();
242
this.SuspendLayout();
243
//
244
// button1
245
//
246
this.button1.Location = new System.Drawing.Point(12, 12);
247
this.button1.Name = "button1";
248
this.button1.Size = new System.Drawing.Size(182, 23);
249
this.button1.TabIndex = 5;
250
this.button1.Text = "获取CPU编号";
251
this.button1.UseVisualStyleBackColor = true;
252
this.button1.Click += new System.EventHandler(this.button1_Click);
253
//
254
// button2
255
//
256
this.button2.Location = new System.Drawing.Point(12, 41);
257
this.button2.Name = "button2";
258
this.button2.Size = new System.Drawing.Size(186, 23);
259
this.button2.TabIndex = 17;
260
this.button2.Text = "获取计算机CPU的当前电压";
261
this.button2.UseVisualStyleBackColor = true;
262
this.button2.Click += new System.EventHandler(this.button2_Click);
263
//
264
// button3
265
//
266
this.button3.Location = new System.Drawing.Point(12, 70);
267
this.button3.Name = "button3";
268
this.button3.Size = new System.Drawing.Size(186, 23);
269
this.button3.TabIndex = 18;
270
this.button3.Text = "获取计算机CPU的外部频率";
271
this.button3.UseVisualStyleBackColor = true;
272
this.button3.Click += new System.EventHandler(this.button3_Click);
273
//
274
// button4
275
//
276
this.button4.Location = new System.Drawing.Point(12, 99);
277
this.button4.Name = "button4";
278
this.button4.Size = new System.Drawing.Size(186, 23);
279
this.button4.TabIndex = 19;
280
this.button4.Text = "获取计算机CPU的二级缓存";
281
this.button4.UseVisualStyleBackColor = true;
282
this.button4.Click += new System.EventHandler(this.button4_Click);
283
//
284
// button5
285
//
286
this.button5.Location = new System.Drawing.Point(12, 128);
287
this.button5.Name = "button5";
288
this.button5.Size = new System.Drawing.Size(186, 23);
289
this.button5.TabIndex = 21;
290
this.button5.Text = "获取计算机CPU的制造商名称";
291
this.button5.UseVisualStyleBackColor = true;
292
this.button5.Click += new System.EventHandler(this.button5_Click);
293
//
294
// button6
295
//
296
this.button6.Location = new System.Drawing.Point(204, 157);
297
this.button6.Name = "button6";
298
this.button6.Size = new System.Drawing.Size(206, 23);
299
this.button6.TabIndex = 22;
300
this.button6.Text = "获取计算机CPU的产品名称";
301
this.button6.UseVisualStyleBackColor = true;
302
this.button6.Click += new System.EventHandler(this.button6_Click);
303
//
304
// button7
305
//
306
this.button7.Location = new System.Drawing.Point(12, 158);
307
this.button7.Name = "button7";
308
this.button7.Size = new System.Drawing.Size(186, 23);
309
this.button7.TabIndex = 23;
310
this.button7.Text = "获取计算机CPU的版本信息";
311
this.button7.UseVisualStyleBackColor = true;
312
this.button7.Click += new System.EventHandler(this.button7_Click);
313
//
314
// button8
315
//
316
this.button8.Location = new System.Drawing.Point(204, 70);
317
this.button8.Name = "button8";
318
this.button8.Size = new System.Drawing.Size(204, 23);
319
this.button8.TabIndex = 24;
320
this.button8.Text = "获取计算机CPU的当前使用百分比";
321
this.button8.UseVisualStyleBackColor = true;
322
this.button8.Click += new System.EventHandler(this.button8_Click);
323
//
324
// button9
325
//
326
this.button9.Location = new System.Drawing.Point(204, 41);
327
this.button9.Name = "button9";
328
this.button9.Size = new System.Drawing.Size(204, 23);
329
this.button9.TabIndex = 25;
330
this.button9.Text = "获取计算机CPU的最大时钟频率";
331
this.button9.UseVisualStyleBackColor = true;
332
this.button9.Click += new System.EventHandler(this.button9_Click);
333
//
334
// button10
335
//
336
this.button10.Location = new System.Drawing.Point(202, 12);
337
this.button10.Name = "button10";
338
this.button10.Size = new System.Drawing.Size(204, 23);
339
this.button10.TabIndex = 26;
340
this.button10.Text = "获取计算机CPU的当前时钟频率";
341
this.button10.UseVisualStyleBackColor = true;
342
this.button10.Click += new System.EventHandler(this.button10_Click);
343
//
344
// button11
345
//
346
this.button11.Location = new System.Drawing.Point(204, 99);
347
this.button11.Name = "button11";
348
this.button11.Size = new System.Drawing.Size(204, 23);
349
this.button11.TabIndex = 27;
350
this.button11.Text = "获取计算机的CPU地址宽度";
351
this.button11.UseVisualStyleBackColor = true;
352
this.button11.Click += new System.EventHandler(this.button11_Click);
353
//
354
// button14
355
//
356
this.button14.Location = new System.Drawing.Point(204, 128);
357
this.button14.Name = "button14";
358
this.button14.Size = new System.Drawing.Size(204, 23);
359
this.button14.TabIndex = 28;
360
this.button14.Text = "获取计算机的CPU数据宽度";
361
this.button14.UseVisualStyleBackColor = true;
362
this.button14.Click += new System.EventHandler(this.button14_Click);
363
//
364
// label1
365
//
366
this.label1.AutoSize = true;
367
this.label1.Location = new System.Drawing.Point(15, 193);
368
this.label1.Name = "label1";
369
this.label1.Size = new System.Drawing.Size(341, 12);
370
this.label1.TabIndex = 29;
371
this.label1.Text = "作者:清清月儿 http://blog.csdn.net/21aspnet/ 2007.3.23";
372
//
373
// Form1
374
//
375
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
376
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
377
this.ClientSize = new System.Drawing.Size(422, 214);
378
this.Controls.Add(this.label1);
379
this.Controls.Add(this.button14);
380
this.Controls.Add(this.button11);
381
this.Controls.Add(this.button10);
382
this.Controls.Add(this.button9);
383
this.Controls.Add(this.button8);
384
this.Controls.Add(this.button7);
385
this.Controls.Add(this.button6);
386
this.Controls.Add(this.button5);
387
this.Controls.Add(this.button4);
388
this.Controls.Add(this.button3);
389
this.Controls.Add(this.button2);
390
this.Controls.Add(this.button1);
391
this.Name = "Form1";
392
this.Text = "Form1";
393
this.ResumeLayout(false);
394
this.PerformLayout();
395
396
}
397
398
#endregion
399
400
private System.Windows.Forms.Button button1;
401
private System.Windows.Forms.Button button2;
402
private System.Windows.Forms.Button button3;
403
private System.Windows.Forms.Button button4;
404
private System.Windows.Forms.Button button5;
405
private System.Windows.Forms.Button button6;
406
private System.Windows.Forms.Button button7;
407
private System.Windows.Forms.Button button8;
408
private System.Windows.Forms.Button button9;
409
private System.Windows.Forms.Button button10;
410
private System.Windows.Forms.Button button11;
411
private System.Windows.Forms.Button button14;
412
private System.Windows.Forms.Label label1;
413
}
414
}
415
416
第二个
CpuInfo.cs

using System;
using System.Configuration;
using System.Runtime.InteropServices;


/**//**//**//**
* LayoutKind.Automatic:为了提高效率允许运行态对类型成员重新排序
* 注意:永远不要使用这个选项来调用不受管辖的动态链接库函数。
* LayoutKind.Explicit:对每个域按照FieldOffset属性对类型成员排序
* LayoutKind.Sequential:对出现在受管辖类型定义地方的不受管辖内存中的类型成员进行排序。
*/

/**//**//**//// <summary>
/// 定义CPU的信息结构
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct CpuInfo



{

/**//**//**//// <summary>
/// OEM ID
/// </summary>
public uint dwOemId;

/**//**//**//// <summary>
/// 页面大小
/// </summary>
public uint dwPageSize;
public uint lpMinimumApplicationAddress;
public uint lpMaximumApplicationAddress;
public uint dwActiveProcessorMask;

/**//**//**//// <summary>
/// CPU个数
/// </summary>
public uint dwNumberOfProcessors;

/**//**//**//// <summary>
/// CPU类型
/// </summary>
public uint dwProcessorType;
public uint dwAllocationGranularity;

/**//**//**//// <summary>
/// CPU等级
/// </summary>
public uint dwProcessorLevel;
public uint dwProcessorRevision;
}
MemoryInfo.cs
using System;
using System.Configuration;
using System.Runtime.InteropServices;


/**//**//**//**
* LayoutKind.Automatic:为了提高效率允许运行态对类型成员重新排序
* 注意:永远不要使用这个选项来调用不受管辖的动态链接库函数。
* LayoutKind.Explicit:对每个域按照FieldOffset属性对类型成员排序
* LayoutKind.Sequential:对出现在受管辖类型定义地方的不受管辖内存中的类型成员进行排序。
*/

/**//**//**//// <summary>
/// 定义内存的信息结构
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct MemoryInfo



{

/**//**//**//// <summary>
///
/// </summary>
public uint dwLength;

/**//**//**//// <summary>
/// 已经使用的内存
/// </summary>
public uint dwMemoryLoad;

/**//**//**//// <summary>
/// 总物理内存大小
/// </summary>
public uint dwTotalPhys;

/**//**//**//// <summary>
/// 可用物理内存大小
/// </summary>
public uint dwAvailPhys;

/**//**//**//// <summary>
/// 交换文件总大小
/// </summary>
public uint dwTotalPageFile;

/**//**//**//// <summary>
/// 可用交换文件大小
/// </summary>
public uint dwAvailPageFile;

/**//**//**//// <summary>
/// 总虚拟内存大小
/// </summary>
public uint dwTotalVirtual;

/**//**//**//// <summary>
/// 可用虚拟内存大小
/// </summary>
public uint dwAvailVirtual;
}
SystemTimeInfo.cs
using System;
using System.Configuration;
using System.Runtime.InteropServices;


/**//**//**//**
* LayoutKind.Automatic:为了提高效率允许运行态对类型成员重新排序
* 注意:永远不要使用这个选项来调用不受管辖的动态链接库函数。
* LayoutKind.Explicit:对每个域按照FieldOffset属性对类型成员排序
* LayoutKind.Sequential:对出现在受管辖类型定义地方的不受管辖内存中的类型成员进行排序。
*/

/**//**//**//// <summary>
/// 定义系统时间的信息结构
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct SystemTimeInfo



{

/**//**//**//// <summary>
/// 年
/// </summary>
public ushort wYear;

/**//**//**//// <summary>
/// 月
/// </summary>
public ushort wMonth;

/**//**//**//// <summary>
/// 星期
/// </summary>
public ushort wDayOfWeek;

/**//**//**//// <summary>
/// 天
/// </summary>
public ushort wDay;

/**//**//**//// <summary>
/// 小时
/// </summary>
public ushort wHour;

/**//**//**//// <summary>
/// 分钟
/// </summary>
public ushort wMinute;

/**//**//**//// <summary>
/// 秒
/// </summary>
public ushort wSecond;

/**//**//**//// <summary>
/// 毫秒
/// </summary>
public ushort wMilliseconds;
}
另外还定义了一个调用类SystemInfo.cs,代码如下:
using System;
using System.Configuration;
using System.Runtime.InteropServices;
using System.Management;
using System.Text;


/**//**//**//// <summary>
/// SystemInfo 的摘要说明
/// </summary>
public class SystemInfo



{
private const int CHAR_COUNT = 128;
public SystemInfo()


{
}
[DllImport("kernel32")]
private static extern void GetWindowsDirectory(StringBuilder WinDir, int count);

[DllImport("kernel32")]
private static extern void GetSystemDirectory(StringBuilder SysDir, int count);

[DllImport("kernel32")]
private static extern void GetSystemInfo(ref CpuInfo cpuInfo);

[DllImport("kernel32")]
private static extern void GlobalMemoryStatus(ref MemoryInfo memInfo);

[DllImport("kernel32")]
private static extern void GetSystemTime(ref SystemTimeInfo sysInfo);


/**//**//**//// <summary>
/// 查询CPU编号
/// </summary>
/// <returns></returns>
public string GetCpuId()


{
ManagementClass mClass = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mClass.GetInstances();
string cpuId=null;
foreach (ManagementObject mo in moc)


{
cpuId = mo.Properties["ProcessorId"].Value.ToString();
break;
}
return cpuId;
}


/**//**//**//// <summary>
/// 查询硬盘编号
/// </summary>
/// <returns></returns>
public string GetMainHardDiskId()


{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
String hardDiskID=null;
foreach (ManagementObject mo in searcher.Get())


{
hardDiskID = mo["SerialNumber"].ToString().Trim();
break;
}
return hardDiskID;
}


/**//**//**//// <summary>
/// 获取Windows目录
/// </summary>
/// <returns></returns>
public string GetWinDirectory()


{
StringBuilder sBuilder = new StringBuilder(CHAR_COUNT);
GetWindowsDirectory(sBuilder, CHAR_COUNT);
return sBuilder.ToString();
}


/**//**//**//// <summary>
/// 获取系统目录
/// </summary>
/// <returns></returns>
public string GetSysDirectory()


{
StringBuilder sBuilder = new StringBuilder(CHAR_COUNT);
GetSystemDirectory(sBuilder, CHAR_COUNT);
return sBuilder.ToString();
}


/**//**//**//// <summary>
/// 获取CPU信息
/// </summary>
/// <returns></returns>
public CpuInfo GetCpuInfo()


{
CpuInfo cpuInfo = new CpuInfo();
GetSystemInfo(ref cpuInfo);
return cpuInfo;
}


/**//**//**//// <summary>
/// 获取系统内存信息
/// </summary>
/// <returns></returns>
public MemoryInfo GetMemoryInfo()


{
MemoryInfo memoryInfo = new MemoryInfo();
GlobalMemoryStatus(ref memoryInfo);
return memoryInfo;
}


/**//**//**//// <summary>
/// 获取系统时间信息
/// </summary>
/// <returns></returns>
public SystemTimeInfo GetSystemTimeInfo()


{
SystemTimeInfo systemTimeInfo = new SystemTimeInfo();
GetSystemTime(ref systemTimeInfo);
return systemTimeInfo;
}


/**//**//**//// <summary>
/// 获取系统名称
/// </summary>
/// <returns></returns>
public string GetOperationSystemInName()


{
OperatingSystem os = System.Environment.OSVersion;
string osName = "UNKNOWN";
switch (os.Platform)


{
case PlatformID.Win32Windows:
switch (os.Version.Minor)


{
case 0: osName = "Windows 95"; break;
case 10: osName = "Windows 98"; break;
case 90: osName = "Windows ME"; break;
}
break;
case PlatformID.Win32NT:
switch (os.Version.Major)


{
case 3: osName = "Windws NT 3.51"; break;
case 4: osName = "Windows NT 4"; break;
case 5: if (os.Version.Minor == 0)


{
osName = "Windows 2000";
}
else if (os.Version.Minor == 1)


{
osName = "Windows XP";
}
else if (os.Version.Minor == 2)


{
osName = "Windows Server 2003";
}
break;
case 6: osName = "Longhorn"; break;
}
break;
}
return String.Format("{0},{1}", osName, os.Version.ToString());
}
}
以下是调用实例,为了简单,我在一个aspx页面中输出,不过这个程序可以在WinForm中调用:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Runtime.InteropServices;

public partial class Index : System.Web.UI.Page



{
protected void Page_Load(object sender, EventArgs e)


{
if (!Page.IsPostBack)


{
SystemInfo systemInfo = new SystemInfo();
Response.Write("操作系统:" + systemInfo.GetOperationSystemInName() + "<br/>");
Response.Write("CPU编号:"+systemInfo.GetCpuId() + "<br/>");
Response.Write("硬盘编号:"+systemInfo.GetMainHardDiskId() + "<br/>");
Response.Write("Windows目录所在位置:" + systemInfo.GetSysDirectory() + "<br/>");
Response.Write("系统目录所在位置:" + systemInfo.GetWinDirectory() + "<br/>");
MemoryInfo memoryInfo = systemInfo.GetMemoryInfo();
CpuInfo cpuInfo = systemInfo.GetCpuInfo();
Response.Write("dwActiveProcessorMask" + cpuInfo.dwActiveProcessorMask + "<br/>");
Response.Write("dwAllocationGranularity" + cpuInfo.dwAllocationGranularity + "<br/>");
Response.Write("CPU个数:" + cpuInfo.dwNumberOfProcessors + "<br/>");
Response.Write("OEM ID:" + cpuInfo.dwOemId + "<br/>");
Response.Write("页面大小" + cpuInfo.dwPageSize + "<br/>");
Response.Write("CPU等级" + cpuInfo.dwProcessorLevel + "<br/>");
Response.Write("dwProcessorRevision" + cpuInfo.dwProcessorRevision + "<br/>");
Response.Write("CPU类型" + cpuInfo.dwProcessorType + "<br/>");
Response.Write("lpMaximumApplicationAddress" + cpuInfo.lpMaximumApplicationAddress + "<br/>");
Response.Write("lpMinimumApplicationAddress" + cpuInfo.lpMinimumApplicationAddress + "<br/>");
Response.Write("CPU类型:" + cpuInfo.dwProcessorType + "<br/>");
Response.Write("可用交换文件大小:" + memoryInfo.dwAvailPageFile + "<br/>");
Response.Write("可用物理内存大小:" + memoryInfo.dwAvailPhys + "<br/>");
Response.Write("可用虚拟内存大小" + memoryInfo.dwAvailVirtual + "<br/>");
Response.Write("操作系统位数:" + memoryInfo.dwLength + "<br/>");
Response.Write("已经使用内存大小:" + memoryInfo.dwMemoryLoad + "<br/>");
Response.Write("交换文件总大小:" + memoryInfo.dwTotalPageFile + "<br/>");
Response.Write("总物理内存大小:" + memoryInfo.dwTotalPhys + "<br/>");
Response.Write("总虚拟内存大小:" + memoryInfo.dwTotalVirtual + "<br/>");
}
}
}
