c# 獲取硬件配置信息

 

 
using System;
using System.Management;
using System.Collections;
using System.Collections.Specialized;
using System.Text;

namespace Management
{
    
WMIPath

    
/// <summary> 
    
/// 獲取系統信息 
    
/// </summary> 
    
/// <example> 
    
/// <code> 
    
/// WMI w = new WMI(WMIPath.Win32_NetworkAdapterConfiguration); 
    
/// for (int i = 0; i < w.Count; i ) 
    /// { 
    
/// if ((bool)w[i, "IPEnabled"]) 
    
/// { 
    
/// Console.WriteLine("Caption:{0}", w[i, "Caption"]); 
    
/// Console.WriteLine("MAC Address:{0}", w[i, "MACAddress"]); 
    
/// } 
    
/// } 
    
/// </code> 
    
/// </example> 

    public sealed class WMI
    
{
        
private ArrayList mocs;
        
private StringDictionary names; // 用來存儲屬性名,便於忽略大小寫查詢正確名稱。 

        
/// <summary> 
        
/// 信息集合數量 
        
/// </summary> 

        public int Count
        
{
            
get return mocs.Count; }
        }


        
/// <summary> 
        
/// 獲取指定屬性值,注意某些結果可能是數組。 
        
/// </summary> 

        public object this[int index, string propertyName]
        
{
            
get
            
{
                
try
                
{
                    
string trueName = names[propertyName.Trim()]; // 以此可不區分大小寫獲得正確的屬性名稱。 
                    Hashtable h = (Hashtable)mocs[index];
                    
return h[trueName];
                }

                
catch
                
{
                    
return null;
                }

            }

        }


        
/// <summary> 
        
/// 返回所有屬性名稱。 
        
/// </summary> 
        
/// <param name="index"></param> 
        
/// <returns></returns> 

        public string[] PropertyNames(int index)
        
{
            
try
            
{
                Hashtable h 
= (Hashtable)mocs[index];
                
string[] result = new string[h.Keys.Count];

                h.Keys.CopyTo(result, 
0);

                Array.Sort(result);
                
return result;
            }

            
catch
            
{
                
return null;
            }

        }


        
/// <summary> 
        
/// 返回測試信息。 
        
/// </summary> 
        
/// <returns></returns> 

        public string Test() 
        

            
try 
            

                StringBuilder result 
= new StringBuilder(1000); 

                
for (int i = 0; i < Count; i ) 
                

                    
int j = 0
                    
foreach(string s in PropertyNames(i)) 
                    

                        result.Append(
string.Format("{0}:{1}={2} ", j, s, this[i, s])); 

                        
if (this[i, s] is Array) 
                        

                            Array v1 
= this[i, s] as Array; 
                            
for (int x = 0; x < v1.Length; x ) 
                            

                             result.Append(
" " v1.GetValue(x) " "); 
                            }
 
                        }
 
                    }
 
                    result.Append(
"======WMI======= "); 
                }
 

                
return result.ToString(); 
            }
 
            
catch 
            

                
return string.Empty; 
            }
 
        }


        
/// <summary> 
        
/// 構造函數 
        
/// </summary> 
        
/// <param name="path"></param> 

        public WMI(string path)
        
{
            names 
= new StringDictionary();
            mocs 
= new ArrayList();

            
try
            
{
                ManagementClass cimobject 
= new ManagementClass(path);
                ManagementObjectCollection moc 
= cimobject.GetInstances();

                
bool ok = false;
                
foreach (ManagementObject mo in moc)
                
{
                    Hashtable o 
= new Hashtable();
                    mocs.Add(o);

                    
foreach (PropertyData p in mo.Properties)
                    
{
                        o.Add(p.Name, p.Value);
                        
if (!ok) names.Add(p.Name, p.Name);
                    }


                    ok 
= true;
                    mo.Dispose();
                }

                moc.Dispose();
            }

            
catch (Exception e)
            
{
                
throw new Exception(e.Message);
            }

        }


        
/// <summary> 
        
/// 構造函數 
        
/// </summary> 
        
/// <param name="path"></param> 

        public WMI(WMIPath path)
            : 
this(path.ToString())
        
{
        }

    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章