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 窗体设计器生成的代码

        
/**//**//**//// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Form1());
        }


    }

}

 原文地址 http://blog.csdn.net/gangliao/archive/2007/12/18/1949373.aspx
posted on 2008-06-26 13:27  Thunderdanky  阅读(1211)  评论(0编辑  收藏  举报