200906260901[C#]抓取系統資訊

C#可以使用ManagementObjectSearcher利用下Query的方式抓取系統的資訊
以下範例為抓取USB資訊
using System.Management;

        private void GetUSBInfo()
        {
            StringBuilder sb = new StringBuilder();
            ManagementObjectSearcher usbDeviceQuery = new ManagementObjectSearcher("SELECT * FROM Win32_USBHub");
            ManagementObjectCollection devices = usbDeviceQuery.Get();
            foreach (ManagementObject usbDevice in devices)
            {
                sb.AppendLine("==========================================================");
                sb.AppendLine("Name : " + usbDevice["Name"].ToString());
                sb.AppendLine("DeviceID : " + usbDevice["DeviceID"].ToString());
            }
            MessageBox.Show(sb.ToString());
        }

----------------------------------------------------------------------------------
            // 抓取硬碟&USB儲存裝置磁碟代號
            using (ManagementClass devs = new ManagementClass(@"Win32_Diskdrive"))
            {
                ManagementObjectCollection moc = devs.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    Console.WriteLine(mo["DeviceId"]);
                    Console.WriteLine(mo["PNPDeviceID"]);
                    foreach (ManagementObject b in mo.GetRelated("Win32_DiskPartition"))
                    {
                        Console.WriteLine("{0}", b["Name"]);
                        foreach (ManagementBaseObject c in b.GetRelated("Win32_LogicalDisk"))
                            Console.WriteLine("{0}", c["Name"]);
                    }
                }
            }

另外WMI Explorer可以讓你當作參考

Windows Management Instrumentation 簡介
MSDN Computer System Hardware Classes

沒有上一則|日誌首頁|沒有下一則
回應
關鍵字
    沒有新回應!
交換連結
台灣網站登錄目錄