using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Management;
using System.Text;
namespace WindowsApplication2
...{
/**//**//**//**//**//**//**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
...{
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader name;
private System.Windows.Forms.ColumnHeader caption;
private System.Windows.Forms.ColumnHeader desc;
private System.Windows.Forms.MenuItem menuItem1;
/**//**//**//**//**//**//**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
...{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
this.getnetwork();
this.getvideo();
this.getsound();
this.getcpu();
this.getmemory();
this.getmainboard();
}
//得到硬件的信息集合
public ManagementObject dosth(string[] hstra)
...{
string hname=hstra[0];
ManagementObjectSearcher query = new ManagementObjectSearcher(hstra[1]);
ManagementObjectCollection qc = query.Get();
foreach( ManagementObject obj in qc)
...{
switch(hname)
...{
case "网卡":
if((bool)obj["ipEnabled"]==false)
continue;
//this.getallinfo(obj);
string cap=(string)obj["Caption"];
string tmp=cap.Substring(1,8);
if(int.Parse(tmp)>100)
continue;
return obj;
case "显卡":
return obj;
case "声卡":
return obj;
case "CPU":
return obj;
case "内存":
return obj;
case "主板":
return obj;
}
}
return null;
}
//得到主板的信息
public void getmainboard()
...{
string mosstr="select * from Win32_BaseBoard";
string[] hstra=...{"主板",mosstr};
ManagementObject obj=this.dosth(hstra);
this.getallinfo(obj);//先用这个命令取出所有的信息,然后再挑选其中所需要的!
}
//得到内存的信息
public void getmemory()
...{
string mosstr="select * from Win32_PhysicalMemory";
string[] hstra=...{"内存",mosstr};
ManagementObject obj=this.dosth(hstra);
//this.getallinfo(obj);//先用这个命令取出所有的信息,然后再挑选其中所需要的!
int memsize=int.Parse(obj["Capacity"].ToString())/(1024*1024);
string []stra=...{hstra[0],obj["Name"].ToString(),memsize.ToString()+"M"};
this.filllist(stra);
}
//得到CPU的信息
public void getcpu()
...{
string mosstr="select * from Win32_Processor";
string[] hstra=...{"CPU",mosstr};
ManagementObject obj=this.dosth(hstra);
//this.getallinfo(obj);//先用这个命令取出所有的信息,然后再挑选其中所需要的!
float fs=float.Parse(obj["CurrentClockSpeed"].ToString())/1000;
string[] stra=...{hstra[0],obj["Name"].ToString(),fs.ToString()+"G"};
this.filllist(stra);
}
//得到声卡的信息
public void getsound()
...{
string mosstr="select * from Win32_SoundDevice";
string[] hstra=...{"声卡",mosstr};
ManagementObject obj=this.dosth(hstra);
//this.getallinfo(obj);//先用这个命令取出所有的信息,然后再挑选其中所需要的!
string[] stra=...{hstra[0],(string)obj["Caption"]};
this.filllist(stra);
}
//得到网卡的信息
public void getnetwork()
...{
string mosstr="SELECT * From Win32_NetworkAdapterConfiguration";
string[] hstra=...{"网卡",mosstr};
ManagementObject obj=this.dosth(hstra);
//this.getallinfo(obj);//先用这个命令取出所有的信息,然后再挑选其中所需要的!
string[] ipadd = (string[])obj["IPAddress"];
System.Text.StringBuilder sb=new StringBuilder();
foreach(string str in ipadd)
...{
sb.Append(str);
}
string[] stra=...{hstra[0],(string)obj["Caption"],sb.ToString()};
this.filllist(stra);
}
//得到显卡信息
public void getvideo()
...{
string mosstr="select * from Win32_VideoController";
string[] hstra=...{"显卡",mosstr};
ManagementObject obj=this.dosth(hstra);
//this.getallinfo(obj);//先用这个命令取出所有的信息,然后再挑选其中所需要的!
int ramsize=(int.Parse(obj["AdapterRAM"].ToString()))/(1024*1024);
string[] stra=...{hstra[0],(string)obj["Caption"],ramsize.ToString()+"M"};
this.filllist(stra);
}
//枚举当前设备所有能显示的信息
public void getallinfo(ManagementObject obj)
...{
System.Management.PropertyDataCollection pdc=obj.Properties;
foreach(PropertyData pd in pdc)
...{
try
...{
string []stra=...{pd.Name,pd.Value.ToString()};
this.filllist(stra);
}
catch
...{}
}
}
//把相关的信息添加到listview
public void filllist(string[] stra)
...{
listView1.Items.Add(new ListViewItem(stra));
}
/**//**//**//**//**//**//**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
...{
if( disposing )
...{
if (components != null)
...{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//**//**//**//**//**//**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.listView1 = new System.Windows.Forms.ListView();
this.name = new System.Windows.Forms.ColumnHeader();
this.caption = new System.Windows.Forms.ColumnHeader();
this.desc = new System.Windows.Forms.ColumnHeader();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] ...{
this.name,
this.caption,
this.desc});
this.listView1.GridLines = true;
this.listView1.LabelEdit = true;
this.listView1.Location = new System.Drawing.Point(8, 8);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(760, 440);
this.listView1.TabIndex = 0;
this.listView1.View = System.Windows.Forms.View.Details;
//
// name
//
this.name.Text = "name";
this.name.Width = 184;
//
// caption
//
this.caption.Text = "caption";
this.caption.Width = 393;
//
// desc
//
this.desc.Text = "desc";
this.desc.Width = 219;
//
// menuItem1
//
this.menuItem1.Index = -1;
this.menuItem1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(776, 453);
this.Controls.Add(this.listView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/**//**//**//**//**//**//**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
...{
Application.Run(new Form1());
}
}
}
|
|