[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Use WMI to get information including the operating system's name in C#


WMI (Windows Management Instrumentation) lets you use SQL-like statements to ask the computer about itself. This example uses it to get:

  • The operating system name including its edition (Home, Ultimate, etc.), version, and Service Pack number
  • The number of logical processors
  • The number of bits the system uses (32 or 64)

When the program loads, it uses the following code to display these values in labels.

// Add a reference to System.Management. using System.Management; ... // Display the operating system's name. private void Form1_Load(object sender, EventArgs e) { // Get the OS information. // For more information from this query, see: // http://msdn.microsoft.com/library/aa394239.aspx string os_query = "SELECT * FROM Win32_OperatingSystem"; ManagementObjectSearcher os_searcher = new ManagementObjectSearcher(os_query); foreach (ManagementObject info in os_searcher.Get()) { lblCaption.Text = info.Properties["Caption"].Value.ToString().Trim(); lblVersion.Text = "Version " + info.Properties["Version"].Value.ToString() + " SP " + info.Properties["ServicePackMajorVersion"].Value.ToString() + "." + info.Properties["ServicePackMinorVersion"].Value.ToString(); } // Get number of processors. // For more information from this query, see: // http://msdn.microsoft.com/library/aa394373.aspx string cpus_query = "SELECT * FROM Win32_ComputerSystem"; ManagementObjectSearcher cpus_searcher = new ManagementObjectSearcher(cpus_query); foreach (ManagementObject info in cpus_searcher.Get()) { lblCpus.Text = info.Properties["NumberOfLogicalProcessors"].Value.ToString() + " processors"; } // Get 32- versus 64-bit. // For more information from this query, see: // http://msdn.microsoft.com/library/aa394373.aspx string proc_query = "SELECT * FROM Win32_Processor"; ManagementObjectSearcher proc_searcher = new ManagementObjectSearcher(proc_query); foreach (ManagementObject info in proc_searcher.Get()) { lblBits.Text = info.Properties["AddressWidth"].Value.ToString() + "-bit"; } }

The code creates a ManagementObjectSearcher to execute the WMI query SELECT * FROM Win32_OperatingSystem to get information about the operating system. It uses the searcher's Get method to get a collection holding the desired information. In this example, the Get call returns a collection holding a single object. The code uses a foreach loop to loop over that object.

Inside the loop, the code gets the returned object's Caption, Version, ServicePackMajorVersion, and ServicePackMinorVersion properties.

Next the program uses similar code to execute the WMI query SELECT * FROM Win32_ComputerSystem to get the number of logical processors.

It finishes by executing the query SELECT * FROM Win32_Processor to determine whether this is a 32-bit or 64-bit operating system.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.