Click here to Skip to main content
Licence 
First Posted 9 Jun 2004
Views 112,315
Bookmarked 59 times

Retrieving Hardware Information in C#

By | 9 Jun 2004 | Article
Using WMI to extract harware information.

Introduction

This article will explain how to use WMI to access hardware information and properties. It will not set properties of hardware, only retrieve information about it.

Getting Started

To get started, start up a new C# project, either a console app or a window, it doesn't really matter. I am going to use a Console App for this article.

Once all that loads up, we need to add a reference to the Windows Management DLL file. To do that, just go to Project->Add Reference. Then scroll down the .NET tab until you find System.Management. Double click on that and it should appear in the lower list box. Click OK. This makes the DLL available for use in the project.

As a side note, this is the way to use any DLL that was made for use in C#, for instance, if you make a DLL yourself and want to use it in another program. This is exactly how to do it. Just add the reference and then add the using 'namespace' directive at the top of your code where namespace is the namespace name in your DLL.

Using System.Management

I always make sure and include the 2 using directives for the management DLL at the beginning of any code I wish to use this for.

using System.Management;
using System.Management.Instrumentation;

Getting the Info

Now, we are ready to get the info. It is a weird process but I will try and explain it the best way I can.

The first step is to set up the query. It is just like a SQL query to a DB.

ManagementObjectSearcher searcher = 
  new ManagementObjectSearcher("Select Name from Win32_CDROMDrive");

That code creates a new ManagementObjectSearcher. The constructor has a few overloads but I always seem to use just this one, which takes a SQL style query into one of the WMI libraries. The rest of the different WMI libraries can be found on Microsoft's site. I'll put the link below.

So now, we have instantiated the query. We now want to go through and get our data. This is a rather simple process. Will will use the foreach operator to go through each of the CD-ROM drives that our query returned.

foreach(ManagementObject cdrom in searcher.Get())
{
   Console.WriteLine("CD-ROM Name: {0}", cdrom.GetPropertyValue("Name"));
}

The GetPropertyValue() function takes the name of the property you wish to access in String format. The only requirement is that you must have included this name in the query from earlier otherwise it will throw an exception.

Sample Program

This is what your final project should look like when you are done. Don't forget to add that reference to System.Management DLL.

using System;
using System.Management;
using System.Management.Instrumentation;

namespace ConsoleApplication1
{
   class Class1
   {
      [STAThread]
      static void Main(string[] args)
      {
         ManagementObjectSearcher searcher = 
           new ManagementObjectSearcher("Select Name from Win32_CDROMDrive");
         foreach(ManagementObject cdrom in searcher.Get())
         {
            Console.WriteLine("Name: {0}\n", 
                          cdrom.GetPropertyValue("Name"));
         }
      }
   }
}

Other WMI

There are tons of properties that can be accessed although some of them don't seem to work sometimes depending on what hardware you have and what not. Check out the main site for all of these:

Some of them are actually really helpful. Just make sure to throw in some exception handling. Otherwise you are in for some real pain.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

jiggafied8

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberpolczym3:25 20 Mar '11  
Generalc# code for retrive CD Serial Number PinmemberBabita Shivade22:48 19 Mar '09  
GeneralPortnumber Pinmemberjbb0821:25 17 Mar '08  
GeneralGood! PinmemberCoder_200714:59 15 Nov '07  
GeneralPlz discuss PinmemberHaroon ur Rasheed21:26 23 Aug '07  
GeneralRe: Plz discuss Pinmemberjiggafied83:21 24 Aug '07  
QuestionRetrieving harddisk name? PinmemberTony Wijaya0:15 2 Mar '07  
AnswerRe: Retrieving harddisk name? PinmemberMohamad K Ayash1:56 6 Sep '07  
QuestionList of Hardwares PinmemberPeter Bryan20:49 12 Nov '06  
AnswerRe: List of Hardwares Pinmemberfbcsymmetry2:02 12 Jul '07  
QuestionHow to retrieve the hardware configuration of remote machine PinmemberAnanth197723:08 8 Nov '06  
GeneralWMI and C# PinmemberJim Barber18:13 26 Jul '05  
GeneralRe: WMI and C# Pinmemberjiggafied84:06 27 Jul '05  
GeneralESRI shapefile reading Pinsussbabar shabbir raja1:46 28 Apr '05  
GeneralRe: ESRI shapefile reading Pinmemberbrownpearl20:47 12 Jan '10  
GeneralWin32_Fan PinmemberYeast2720:07 24 Apr '05  
GeneralRemote machine's priveleges Pinmemberdharani6:07 23 Jun '04  
GeneralRe: Remote machine's priveleges Pinmemberjiggafied89:56 23 Jun '04  
GeneralRe: Remote machine's priveleges Pinmembercoby cai3:28 19 Jan '06  
GeneralBiosNumber Pinmemberunitecsoft8:40 19 Jun '04  
GeneralRe: BiosNumber Pinmembert_barbillon1:39 27 Sep '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 10 Jun 2004
Article Copyright 2004 by jiggafied8
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid