C# ,

C# ,

Hi, everyone. This week, we will discuss about something different. We will use C# to play with the guts of our PCs. C# is a great OOP language and yes, it permits us to do some bare metal stuff with our code. Though in this domain C and C++ are more efficient, we can still use C# to perform such tasks. Recently, I’ve been involved in a project which needed me to work with the PC’s core functionalities. And in this post, I’ll distill what I learned from my researches on the internet in a few tips. I’ll surely make subsequent blog posts where I talk about more functionalities related to a lower level programming than usual.

Let’s Access our PC’s Hardware and Serial Ports With C sharp ManagementObjectSearcher and Registry

What we will be doing here is simple. We will get information about our device’s processor with the Registry. And We will access information about Serial Ports and external devices plugged into these ports using the ManagementObjectSearcher and the CIMWin32 WMI Providers.

Getting information about external devices connected to USB Serial Ports

To accomplish this, we will write a query with the the ManagementObjectSearcher for a WMI Class. This Class represents the external device connected to the PC and will give us the information about the device. we will get the info we need only.

I- Let’s Get information About The PC’s Pointing Devices

You can get information about several devices just by precising the right WMI class and the appropriate query. Here is a demonstration about getting information about the pointing device connected to the PC. Running the demo on a PC with several types of pointing devices will function the same way. Here are the steps to do so.

Query the ManagementObjectSearcher for pointing devices.

for pointing devices. Get the ManagementObjectCollection and loop through every object to get the Property you need.

static void MouseSerialPortUSBInfo() { ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_PointingDevice"); var mao = managementObjectSearcher.Get(); if (mao.Count < 1) { Console.WriteLine("Sorry, but no Pointing device detected..."); } foreach (ManagementObject currentObject in managementObjectSearcher.Get()) { Console.WriteLine("Name : " + currentObject["Name"].ToString()); Console.WriteLine("DeviceID : " + currentObject["DeviceID"].ToString()); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 static void MouseSerialPortUSBInfo ( ) { ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher ( "SELECT * FROM Win32_PointingDevice" ) ; var mao = managementObjectSearcher . Get ( ) ; if ( mao . Count < 1 ) { Console . WriteLine ( "Sorry, but no Pointing device detected..." ) ; } foreach ( ManagementObject currentObject in managementObjectSearcher . Get ( ) ) { Console . WriteLine ( "Name : " + currentObject [ "Name" ] . ToString ( ) ) ; Console . WriteLine ( "DeviceID : " + currentObject [ "DeviceID" ] . ToString ( ) ) ; } }

The code above displays to the console the pointing device’s ID and its Name. You could display a lot more information about the device. Here is a full list of what information you can display. As shown below, the device I used to run the code has only one pointing device (Mouse).

II- Let’s Get Information About the Operating System

Getting info about the OS is quite simple. It is very similar to getting the Pointing Devices.

Query the ManagementObjectSearcher for the OS Win32_OperatingSystem .

for the OS . Get the ManagementObjectCollection and loop through every object to get the Property you need.

static void GetSystemInfo() { ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem"); var mao = managementObjectSearcher.Get(); foreach (ManagementObject managementObject in managementObjectSearcher.Get()) { Console.WriteLine("OS Name : " + managementObject["Caption"].ToString()); Console.WriteLine("OS Architecture : " + managementObject["OSArchitecture"].ToString()); } } 1 2 3 4 5 6 7 8 9 10 11 static void GetSystemInfo ( ) { ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher ( "SELECT * FROM Win32_OperatingSystem" ) ; var mao = managementObjectSearcher . Get ( ) ; foreach ( ManagementObject managementObject in managementObjectSearcher . Get ( ) ) { Console . WriteLine ( "OS Name : " + managementObject [ "Caption" ] . ToString ( ) ) ; Console . WriteLine ( "OS Architecture : " + managementObject [ "OSArchitecture" ] . ToString ( ) ) ; } }

III – Let’s Get Information About the Device’s Processor

This step is different, since we won’t get this information through the Management Object. Instead we will access this information via the Registry. Using C# code, we will go through the OS’s Registries and get information about the processor running on the device.

We Open a specific Registry sub key on the PC.

Get the Processor’s name and basic info then display it on the console.

public static void GetProcessorInfo() { RegistryKey processorName = Registry.LocalMachine.OpenSubKey(@"Hardware\Description\System\CentralProcessor\0", RegistryKeyPermissionCheck.ReadSubTree); if (processorName != null) { if (processorName.GetValue("ProcessorNameString") != null) { Console.WriteLine(processorName.GetValue("ProcessorNameString")); } } } 1 2 3 4 5 6 7 8 9 10 11 12 public static void GetProcessorInfo ( ) { RegistryKey processorName = Registry . LocalMachine . OpenSubKey ( @"Hardware\Description\System\CentralProcessor\0" , RegistryKeyPermissionCheck . ReadSubTree ) ; if ( processorName != null ) { if ( processorName . GetValue ( "ProcessorNameString" ) != null ) { Console . WriteLine ( processorName . GetValue ( "ProcessorNameString" ) ) ; } } }

Conclusion

Above is a brief summary of the whole bunch of information you can get about your Operating system, hardware and peripherals. You can find more information about performing such tasks Here . You may also be interested about how LINQ can be addictive for every .Net Developer click here.

Special Offer

Save 16% on your Microsoft Office 365 subscription Learn More >>

Like this: Like Loading...

Follow me on social media and stay updated