C # tips: query whether the current operating system is 64-bit or 32-bit Through WMI

Source: Internet
Author: User

Prerequisites:

  1. 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.
  2. 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:

  1. Add the reference of System. Management to the reference of the Project;
  2. 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:

  1. Windows Management Instrumentation http://msdn.microsoft.com/en-us/library/aa394582%28v=VS.85%29.aspx
  2. WMI Queries http://msdn.microsoft.com/en-us/library/ms186146%28v=vs.80%29.aspx
  3. Using WMI Class in C # http://www.dreamincode.net/forums/topic/42934-using-wmi-class-in-c%23/
  4. Win32_Processor class
    Http://msdn.microsoft.com/en-us/library/aa394373.aspx
  5. Win32_OperatingSystem class
    Http://msdn.microsoft.com/en-us/library/aa394239.aspx
  6. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.