第一個教學例子就是如何搜尋電腦資訊
注意這邊『\\\\FullComputerName\\root\\cimv2』,將『FullComputerName』改成『"\\\\.\\root\\cimv2"』,『.』只是代表當前的名稱
除非針對特殊電腦使用,否則輸入『.』可能會比較好
using System; using System.Collections.Generic; using System.Linq; using System.Management; using System.Text; namespace ConsoleApplication1 { internal class Program { private static void Main(string[] args) { ManagementScope scope = new ManagementScope( "\\\\.\\root\\cimv2"); scope.Connect(); ObjectQuery query = new ObjectQuery( "SELECT * FROM Win32_OperatingSystem"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); ManagementObjectCollection queryCollection = searcher.Get(); foreach (ManagementObject m in queryCollection) { Console.WriteLine("Computer Name : {0}", m["csname"]); Console.WriteLine("Windows Directory : {0}", m["WindowsDirectory"]); Console.WriteLine("Operating System: {0}", m["Caption"]); Console.WriteLine("Version: {0}", m["Version"]); Console.WriteLine("Manufacturer : {0}", m["Manufacturer"]); } Console.Read(); } } }
參考資料:
http://msdn.microsoft.com/en-us/library/ms257337.aspx