Prerequisites:
- WMI (Windows Management Instrumentation) is a core Management support technology built in Windows operating systems. Currently, WMI is a standard and infrastructure, it allows you to access, configure, manage, and monitor almost all Windows resources, such as disks, Event Logs, files, folders, file systems, network components, operating system settings, performance data, printers, process, registry settings, and so on.
- WQL (WMI Query Language) is the Query Language built in WMI, which is a subset of SQL.
This article describes a small WMI application: Check whether the current operating system is 64-bit or 32-bit.
To run this code, you must:
- Add the reference of System. Management to the reference of the Project;
- You must add a reference to the namespace System. Management in the C # source code file.
using System.Management;
The Code is as follows:
/// <summary>/// Gets OS address width./// </summary>/// <returns>32 indicates 32-bit OS, and 64 indicates 64-bit OS.</returns>public static UInt16 GetOSAddressWidth(){ try { SelectQuery query = new SelectQuery("select AddressWidth from Win32_Processor"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); ManagementObjectCollection moCollection = searcher.Get(); foreach (ManagementObject mo in moCollection) { foreach (PropertyData property in mo.Properties) { if (property.Name.Equals("AddressWidth")) { return Convert.ToUInt16(property.Value); } } } throw new Exception("Didn't get expected query result from WMI."); } catch (Exception ex) { throw new Exception("Error occurs in WMI query.", ex.InnerException); }}
In a 32-bit Windows operating system, this function returns 32; In a 64-bit Windows operating system, this function returns 64.
Currently, no Exception is thrown, unless the current operating system is not Windows or Windows (such as Windows 3.1?) earlier than Windows 95 ?).
However, I think I should consider the situation on Mono on Linux.
References:
- Windows Management Instrumentation http://msdn.microsoft.com/en-us/library/aa394582%28v=VS.85%29.aspx
- WMI Queries http://msdn.microsoft.com/en-us/library/ms186146%28v=vs.80%29.aspx
- Using WMI Class in C # http://www.dreamincode.net/forums/topic/42934-using-wmi-class-in-c%23/
- Win32_Processor class
Http://msdn.microsoft.com/en-us/library/aa394373.aspx
- Win32_OperatingSystem class
Http://msdn.microsoft.com/en-us/library/aa394239.aspx
- The series attribute of the Win32_Processor WMI class returns incorrect values. Some processors are run on a Windows Server 2003-based computer.
Http://support.microsoft.com/kb/924779/zh-cn