using System;
using System.Management;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
namespace Rainsoft.Management
{
#region WMIPath
public enum WMIPath
{
// 硬體
Win32_Processor, // CPU 處理器
Win32_PhysicalMemory, // 實體記憶體條
Win32_Keyboard, // 鍵盤
Win32_PointingDevice, // 點輸入裝置,包括滑鼠。
Win32_FloppyDrive, // 軟碟機
Win32_DiskDrive, // 硬碟
Win32_CDROMDrive, // 光碟片磁碟機
Win32_BaseBoard, // 主板
Win32_BIOS, // BIOS 晶片
Win32_ParallelPort, // 並口
Win32_SerialPort, // 串口
Win32_SerialPortConfiguration, // 串口配置
Win32_SoundDevice, // 多媒體設定,一般指音效卡。
Win32_SystemSlot, // 主板插槽 (ISA & PCI & AGP)
Win32_USBController, // USB 控制器
Win32_NetworkAdapter, // 網路介面卡
Win32_NetworkAdapterConfiguration, // 網路介面卡設定
Win32_Printer, // 印表機
Win32_PrinterConfiguration, // 印表機設定
Win32_PrintJob, // 印表機任務
Win32_TCPIPPrinterPort, // 印表機連接埠
Win32_POTSModem, // MODEM
Win32_POTSModemToSerialPort, // MODEM 連接埠
Win32_DesktopMonitor, // 顯示器
Win32_DisplayConfiguration, // 顯卡
Win32_DisplayControllerConfiguration, // 顯卡設定
Win32_VideoController, // 顯卡細節。
Win32_VideoSettings, // 顯卡支援的顯示模式。
// 作業系統
Win32_TimeZone, // 時區
Win32_SystemDriver, // 驅動程式
Win32_DiskPartition, // 磁碟分割
Win32_LogicalDisk, // 邏輯磁碟
Win32_LogicalDiskToPartition, // 邏輯磁碟所在分區及始末位置。
Win32_LogicalMemoryConfiguration, // 邏輯記憶體配置
Win32_PageFile, // 系統頁檔案資訊
Win32_PageFileSetting, // 頁檔案設定
Win32_BootConfiguration, // 系統啟動配置
Win32_ComputerSystem, // 電腦資訊簡要
Win32_OperatingSystem, // 作業系統資訊
Win32_StartupCommand, // 系統自動啟動程式
Win32_Service, // 系統安裝的服務
Win32_Group, // 系統管理組
Win32_GroupUser, // 系統組帳號
Win32_UserAccount, // 使用者帳號
Win32_Process, // 系統進程
Win32_Thread, // 系統線程
Win32_Share, // 共用
Win32_NetworkClient, // 已安裝的網路用戶端
Win32_NetworkProtocol, // 已安裝的網路通訊協定
}
#endregion
/// <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}/n", ++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("/t" + v1.GetValue(x) + "/n");
}
}
}
result.Append("======WMI=======/n");
}
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())
{
}
}
}