Learn WMI

xiaoxiao2021-03-06  179

Through the two-day study, WMI is really a good thing :) Users, such as some OEM vendors to manage and detect products on the production line, with it, from this to those cumbersome APIs, Byebye ...

The following small example is my two days of learning experience:

// Get LAN MAC addressstatic void GetLanMACAddress () {ManagementClass mc = new ManagementClass ( "Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances (); foreach (ManagementObject mo in moc) {if ((bool) mo [ "IPEnabled"] == True) Console.writeline ("Mac Address / T {0}", Mo ["Macaddress"]. TOSTRING ()); mo.dispose ();} console.writeline ("/ n / r");} // Get HDD Size / Serial # / Firmwarestatic void GetHDDInfo () {String HDid; ManagementClass cimobject = new ManagementClass ( "Win32_DiskDrive"); ManagementObjectCollection moc = cimobject.GetInstances (); foreach (ManagementObject mo in moc) {HDid = (string ) mo.properties ["model"]. Value;

//Console.writeline ("HDD Serial / T {0}", Mo ["Serial Number"]. TOSTRING ());

Console.writeline ("HDD Size / T {0} Bytes", Mo ["Size"]. TOSTRING ()); console.writeline ("HDD model / t {0}", Mo ["Model"]. Tostring ))); Console.writeline ("HDD Manufacturer / T {0}", Mo ["Manufacturer"]. ToString ()); // console.writeline ("HDD Model / T {0}", Mo ["Firmware" ] .ToString ());

mo.dispose ();

Console.WriteLine ( "/ n / r");} // Get CPU INFO.static void GetCpuInfo () {// string cpuInfo = ""; // cpu SEQ ID ManagementClass cimobject = new ManagementClass ( "Win32_Processor"); ManagementObjectCollection MOC = CIMOBJECT.GETInstances (); Foreach (ManagementObject Mo in moc) {// cpuinfo = mo.Properties ["processorid"]. Value.toString (); console.writeline ("Processorid: / t {0}", Mo ["Processorid"]. TOSTRING ()); console.writeline ("currentclockspeed: / t {0}", Mo ["currentclockspeed"]. Tostring ()); // console.writeline ("L2Cachesize: / t {0 } ", Mo [" L2CACHESIZE "]. TOSTRING ()); // Console.writeline (" L2Cachespeed: / T {0} ", Mo [" L2CACHESPEED "]. TOSTRING ());

Console.writeline ("Description: / T {0}", Mo ["Description"]. TOSTRING ()); console.writeline ("Processortype: / T {0}", Mo ["ProcessORTYPE"]. Tostring () Console.Writeline ("Version: / T {0}", Mo ["Version"]. TOSTRING ()); console.writeline ("Revision: / T {0}", Mo ["revision"]. TOSTRING ()); Console.writeline ("Manufacturer: / T {0}", Mo ["Manufacturer"]. TOSTRING ()); // console.writeLine (cpuinfo);} console.writeline ("/ n / r" );} // Get cahce INFO.static void GetCacheInfo () {ManagementClass cimobject = new ManagementClass ( "Win32_CacheMemory"); ManagementObjectCollection moc = cimobject.GetInstances (); foreach (ManagementObject mo in moc) {Console.WriteLine ( "MaxCacheSize: / t {0} ", Mo [" MaxCachesize "]. TOSTRING ()); console.writeline (" location: / t {0} ", Mo [" purpose "]. toString ());} console.writeline "/ n / r");} // Get Memory INFO.static void GetMemoryInfo () {ManagementClass cimobject = new ManagementClass ( "Win32_PhysicalMemory"); ManagementObjectCollection moc = cimobject.GetInstances (); foreach (ManagementObject mo in moc) {Console .Writeline ("CAPAC) ITY: / T {0} ", Mo [" Capacity "]. TOSTRING ()); console.writeline (" DeviceLocator: / T {0} ", Mo [" DeviceLocator "]. Tostring ()); console.writeline ("Description: / T {0}", Mo ["Description"]. TOSTRING ());} console.writeLine ("/ n / r");} // Get the battery info.static void getBatteryInfo () { ManagementClass cimobject = new ManagementClass ( "Win32_Battery"); ManagementObjectCollection moc = cimobject.GetInstances (); foreach (ManagementObject mo in moc) {// Console.WriteLine ( "Capacity: / t {0}", mo [ "BatteryRechargeTime"] .Tostring ());

//Console.writeline ("DEVICELOCATOR: (il (}", Mo ["Batterystatus"]. TOSTRING ()); console.writeline ("Description: / T {0}", Mo ["Chemistry"]. Tostring ());} Console.WriteLine ( "/ n / r");} // Get the LCD INFO.static void GetLCDInfo () {ManagementClass cimobject = new ManagementClass ( "Win32_DesktopMonitor"); ManagementObjectCollection moc = cimobject.GetInstances () Foreach (ManagementObject Mo In MoC) {Console.writeline ("Description: / T {0}", Mo ["Description"]. Tostring ()); console.writeline ("PixelsperxLogicalInch: / T {0}", MO ["PixelsperXLogicalinch"]. TOSTRING ()); console.writeline ("Pixelspers", Mo ["Pixelspers"]. TOSTRING ()); console.writeline ("Manufactural: / t {0}" , Mo ["MonitorManuFacturer"]. TOSTRING ()); // [】 "," MonitorType :/t{} ", Mo [" MonitorType "]. toString ()); console.writeline (" ScreenHeight: / t {0} ", Mo [" ScreenHeight "]. TOSTRING ()); console.writeline (" ScreenWidth: / T {0} ", Mo [" Screenwidth "]. Tostring ()); // DateTime to string } Console.writeLine ("/ n / r");} // get the bios info.static vo id GetBIOSInfo () {ManagementClass cimobject = new ManagementClass ( "Win32_BIOS"); ManagementObjectCollection moc = cimobject.GetInstances (); foreach (ManagementObject mo in moc) {Console.WriteLine ( "Version: / t {0}", mo [ " Version "]. TOSTRING ()); // console.writeline (" DeviceLocator: / T {0} ", Mo [" buildNumber "]. Tostring ()); console.writeline (" Biosversion: / t {0} " , Mo ["BiosVersion"]. TOSTRING ()); // [【【【─ ─}:}, Mo ["Manufacturer"]. Tostring ()); console.writeline

SerialNumber: / T {0} ", Mo [" serialnumber "]. TOSTRING ()); // console.writeline (" installdate: / t {0} ", (Mo [" installdate "]. TOSTRING ()); // datetime to string} Console.WriteLine ( "/ n / r");} // Get HDD SerialNumpublic static void GetHd () {ManagementObjectSearcher wmiSearcher = new ManagementObjectSearcher (); wmiSearcher.Query = new SelectQuery ( "Win32_DiskDrive", " ", new string [] {" PNPDeviceID "}); ManagementObjectCollection myCollection = wmiSearcher.Get (); ManagementObjectCollection.ManagementObjectEnumerator em = myCollection.GetEnumerator (); em.MoveNext (); ManagementBaseObject mo = em.Current; string id = mo .Properties ["pnpDeviceid"]. Value.toString (). Trim (); console.writeline ("My HardDisk is:" id);} // Get Monitor Info.public static void getmonitorinfo () {managementClass Cimobject = New ManagementClass ( "Win32_DisplayConfiguration"); ManagementObjectCollection moc = cimobject.GetInstances (); foreach (ManagementObject mo in moc) {Console.WriteLine ( "Description: / t {0}"., mo [ "Description"] ToString ()); Console.writeLine ("DisplayFrequency: / T {0}", Mo ["DisplayFrequency"]. TOSTRING ()); console.writeline ("PelsHeight: / T {0}", Mo ["Pelsheight"]. ToString ()); console .Writeline ("Pelswidth: / T {0}", Mo ["PELSWIDTH"]. ToString ()); // [//console.writeline ", MO [" MonitorType " ] .Tostring ()); // console.writeline ("ScreenHeight: / T {0}", Mo ["ScreenHeight"]. TOSTRING ()); // Console.WriteLine ("ScreenWidth: / t {0}" , Mo ["ScreenWidth"]. TOSTRING ()); // DateTime to string} console.writeLine ("/ n / r");

}

转载请注明原文地址:https://www.9cbs.com/read-128562.html

New Post(0)