Click here to Skip to main content
15,888,610 members
Articles / Programming Languages / C#
Article

Retrieving Hardware Information in C#

Rate me:
Please Sign up or sign in to vote.
4.54/5 (19 votes)
9 Jun 20042 min read 203.1K   67   22
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.

C#
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.

C#
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.

C#
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.

C#
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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNice Article Pin
Rajesh Kumar Sowntararajan18-Jun-13 0:02
professionalRajesh Kumar Sowntararajan18-Jun-13 0:02 
GeneralMy vote of 5 Pin
polczym20-Mar-11 3:25
polczym20-Mar-11 3:25 
Generalc# code for retrive CD Serial Number Pin
Babita Shivade19-Mar-09 22:48
Babita Shivade19-Mar-09 22:48 
GeneralPortnumber Pin
jbb0817-Mar-08 21:25
jbb0817-Mar-08 21:25 
GeneralGood! Pin
Coder_200715-Nov-07 14:59
Coder_200715-Nov-07 14:59 
GeneralPlz discuss Pin
Haroon ur Rasheed23-Aug-07 21:26
Haroon ur Rasheed23-Aug-07 21:26 
GeneralRe: Plz discuss Pin
jiggafied824-Aug-07 3:21
jiggafied824-Aug-07 3:21 
QuestionRetrieving harddisk name? Pin
Tony Wijaya2-Mar-07 0:15
Tony Wijaya2-Mar-07 0:15 
AnswerRe: Retrieving harddisk name? Pin
Mohamad K. Ayyash6-Sep-07 1:56
Mohamad K. Ayyash6-Sep-07 1:56 
QuestionList of Hardwares Pin
Peter Bryan12-Nov-06 20:49
Peter Bryan12-Nov-06 20:49 
AnswerRe: List of Hardwares Pin
fbcsymmetry-hinet12-Jul-07 2:02
fbcsymmetry-hinet12-Jul-07 2:02 
list key word , data ref msdn
you can replace keyword in C# pgm,try it!tks!

Win32_1394Controller
Win32_1394ControllerDevice
Win32_AllocatedResource
Win32_AssociatedBattery
Win32_AssociatedProcessorMemory
Win32_AutochkSetting
Win32_BaseBoard
Win32_Battery
Win32_BIOS
Win32_Bus
Win32_CacheMemory
Win32_CDROMDrive <----------- C# pgm use this keyworkLaugh | :laugh: Laugh | :laugh:
Win32_ControllerHasHub
Win32_CurrentProbe
Win32_DefragAnalysis
Win32_DesktopMonitor
Win32_DeviceBus
Win32_DeviceMemoryAddress
Win32_DeviceSettings
Win32_DiskDrive
Win32_DiskDrivePhysicalMedia
Win32_DisplayConfiguration
Win32_DisplayControllerConfiguration
Win32_DMAChannel
Win32_DriverForDevice
Win32_Fan
Win32_FloppyController
Win32_FloppyDrive
Win32_HeatPipe
Win32_IDEController
Win32_IDEControllerDevice
Win32_InfraredDevice
Win32_IRQResource
Win32_Keyboard
Win32_MemoryArray
Win32_MemoryArrayLocation
Win32_MemoryDevice
Win32_MemoryDeviceArray
Win32_MemoryDeviceLocation
Win32_MotherboardDevice
Win32_MountPoint
Win32_NetworkAdapter
Win32_NetworkAdapterConfiguration
Win32_NetworkAdapterSetting
Win32_OnBoardDevice
Win32_ParallelPort
Win32_PCMCIAController
Win32_PhysicalMedia
Win32_PhysicalMemory
Win32_PhysicalMemoryArray
Win32_PhysicalMemoryLocation
Win32_PnPAllocatedResource
Win32_PnPDevice
Win32_PnPEntity
Win32_PnPSignedDriver
Win32_PnPSignedDriverCIMDataFile
Win32_PointingDevice
Win32_PortableBattery
Win32_PortConnector
Win32_PortResource
Win32_PowerManagementEvent
Win32_POTSModem
Win32_POTSModemToSerialPort
Win32_Printer
Win32_PrinterConfiguration
Win32_PrinterController
Win32_PrinterDriver
Win32_PrinterDriverDll
Win32_PrinterSetting
Win32_PrintJob
Win32_Processor
Win32_Refrigeration
Win32_SCSIController
Win32_SCSIControllerDevice
Win32_SerialPort
Win32_SerialPortConfiguration
Win32_SerialPortSetting
Win32_SMBIOSMemory
Win32_SoundDevice
Win32_SystemBIOS
Win32_SystemDriverPnPEntity
Win32_SystemEnclosure
Win32_SystemMemoryResource
Win32_SystemSlot
Win32_TapeDrive
Win32_TCPIPPrinterPort
Win32_TemperatureProbe
Win32_UninterruptiblePowerSupply
Win32_USBController
Win32_USBControllerDevice
Win32_USBHub
Win32_VideoConfiguration
Win32_VideoController
Win32_VideoSettings
Win32_VoltageProbe
QuestionHow to retrieve the hardware configuration of remote machine Pin
Ananth19778-Nov-06 23:08
Ananth19778-Nov-06 23:08 
GeneralWMI and C# Pin
Jim Barber26-Jul-05 18:13
Jim Barber26-Jul-05 18:13 
GeneralRe: WMI and C# Pin
jiggafied827-Jul-05 4:06
jiggafied827-Jul-05 4:06 
GeneralESRI shapefile reading Pin
babar shabbir raja28-Apr-05 1:46
sussbabar shabbir raja28-Apr-05 1:46 
GeneralRe: ESRI shapefile reading Pin
brownpearl12-Jan-10 20:47
brownpearl12-Jan-10 20:47 
GeneralWin32_Fan Pin
Yeast2724-Apr-05 20:07
Yeast2724-Apr-05 20:07 
GeneralRemote machine's priveleges Pin
dharani23-Jun-04 6:07
dharani23-Jun-04 6:07 
GeneralRe: Remote machine's priveleges Pin
jiggafied823-Jun-04 9:56
jiggafied823-Jun-04 9:56 
GeneralRe: Remote machine's priveleges Pin
coby cai19-Jan-06 3:28
professionalcoby cai19-Jan-06 3:28 
GeneralBiosNumber Pin
unitecsoft19-Jun-04 8:40
unitecsoft19-Jun-04 8:40 
GeneralRe: BiosNumber Pin
t_barbillon27-Sep-06 1:39
t_barbillon27-Sep-06 1:39 

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

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