• How to Get Hardware Information using WMI in C#

    Posted on October 22, 2012 by in C#, Dotnet

    Windows Management Instrumentation (WMI) is the infrastructure for management data and operations on Windows-based operating systems. We can develop applications to obtain enterprise or perform some administrative tasks using WMI. We could use standard WMI queries in .NET Framework applications to enumerate or obtain a collection of WMI objects. WMI Query Language (WQL), a subset of SQL. The query string is stored in a .NET Framework query class such as the SelectQuery class, the WqlEventQuery class, or the RelatedObjectQuery class, and invoked by the ManagementObjectSearcher or ManagementEventWatcher class constructors.

    We use ManagementObjectSearcher to execute the given query to get the list of management objects that matches the select criteria.  When instantiated, an instance of this class takes as input a WMI query represented in an ObjectQuery and execute the query in. When the Get method on this object is invoked, the ManagementObjectSearcher executes the given query in the specified scope and returns a collection of management objects that match the query.

    Example 1: How to retrieve processors information?

    public static void RetrieveProcessorInfo()
    {
        //initialize the select query with command text
        SelectQuery query = new SelectQuery(@"Select * from Win32_Processor");
    
        //initialize the searcher with the query it is supposed to execute
        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
        {
            //execute the query                   
            foreach (ManagementObject process in searcher.Get())
            {
                //print process properties
                Console.WriteLine("/*********Processor Information ***************/");
               Console.WriteLine("{0}{1}", "Manufacturer:" , process["Manufacturer"]);
               Console.WriteLine("{0}{1}", "Name:", process["Name"]);
               Console.WriteLine("{0}{1}", "MaxClockSpeed:", process["MaxClockSpeed"]);
               Console.WriteLine("{0}{1}", "ProcessorID:", process["ProcessorID"]);
               Console.WriteLine("{0}{1}", "Revision:", process["Revision"]);
            }
        }
    

    Get Hardware Information using WMI 1

    Example 2: How to retrieve CD ROM DRIVE details?

    public static void RetrieveCDROMProps()
    {
        //initialize the select query with command text
        SelectQuery query = new SelectQuery(@"Select * from Win32_CDROMDrive");
    
        //initialize the searcher with the query it is supposed to execute
        using (ManagementObjectSearcher searcher = new
        ManagementObjectSearcher(query))
        {
            //execute the query                   
            foreach (ManagementObject process in searcher.Get())
            {
                //print CD ROM properties            
                Console.WriteLine("/*********CD ROM Drive Information ***************/");
                Console.WriteLine("{0}{1}", "Name:", process["Name"]);
                Console.WriteLine("{0}{1}", "Caption:", process["Caption"]);
                Console.WriteLine("{0}{1}", "DeviceID:", process["DeviceID"]);
                Console.WriteLine("{0}{1}", "Description:", process["Description"]);
            }
        }
    }
    

    Get Hardware Information using WMI 4

    Example 3:  How to fetch Operating System properties

    public static void RetrieveOperatingSystemProps()
    {
        //initialize the select query with command text
        SelectQuery query = new SelectQuery(@"Select * from Win32_OperatingSystem");
    
        //initialize the searcher with the query it is supposed to execute
        using (ManagementObjectSearcher searcher = new
        ManagementObjectSearcher(query))
        {
            //execute the query                   
            foreach (ManagementObject process in searcher.Get())
            {
                //print OS properties
                Console.WriteLine("/*********Operating System Information ***************/");
                Console.WriteLine("{0}{1}", "Caption:", process["Caption"]);
                Console.WriteLine("{0}{1}", "ServicePackMajorVersion :", process["ServicePackMajorVersion"]);
                Console.WriteLine("{0}{1}", "ServicePackMinorVersion:", process["ServicePackMinorVersion"]);
                Console.WriteLine("{0}{1}", "InstallDate:", process["InstallDate"]);
                Console.WriteLine("{0}{1}", "Version:", process["Version"]);
    Console.WriteLine("{0}{1}", "FreePhysicalMemory:", process["FreePhysicalMemory"]);
            }
        }
    }
    

    Get Hardware Information using WMI 2

    Example 4:  How to fetch Computer properties (System Info)

    public static void RetrieveComputerProps()
    {
        //initialize the select query with command text
        SelectQuery query = new SelectQuery(@"Select * from Win32_ComputerSystem");
    
        //initialize the searcher with the query it is supposed to execute
        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
        {
            //execute the query                   
            foreach (ManagementObject process in searcher.Get())
            {
                //print system info
                process.Get();
                Console.WriteLine("/*********Operating System Information ***************/");
                Console.WriteLine("{0}{1}", "Caption :", process["Caption"]);
                Console.WriteLine("{0}{1}", "Description:", process["Description"]);
                Console.WriteLine("{0}{1}", "Manufacturer:", process["Manufacturer"]);
                Console.WriteLine("{0}{1}", "Model:", process["Model"]);
                Console.WriteLine("{0}{1}", "TotalPhysicalMemory:", process["TotalPhysicalMemory"]);
    
            }
        }
    }
    

    Get Hardware Information using WMI 3

    Be Sociable, Share!

    Written by

    Software architect with over 10 years of proven experience in designing & developing n-tier and web based software applications, for Finance, Telecommunication, Manufacturing, Internet and other Commercial industries. He believes that success depends on one's ability to integrate multiple technologies to solve a simple as well as complicated problem.

    View all articles by

    Email : [email protected]

    Leave a Reply