Click here to Skip to main content
15,891,316 members
Articles / Programming Languages / C#
Tip/Trick

Obtain Hardware Configuration Using C# and .NET

Rate me:
Please Sign up or sign in to vote.
4.18/5 (5 votes)
25 Oct 2016CPOL2 min read 13.4K   187   14   1
Enumerate System Features

Introduction

I always wondered how diagnostic applications obtained the information about my system.

Background

I am writing an application which loads and uses JPG file for comparison and eventually for further treatment. The performance would suffer significantly if I had to load the files again and again, so I started to write a caching application for my JPG files. This application should use as much memory space as possible, without (if possible) forcing the system to use the virtual space on the harddisk. So my first task was to find out how much memory was installed in the current computer.

During my research, I located the necessary methods to obtain all kinds of information about installed hardware, motherboard, devices. Since I did not find the documentation easy to understand, I want to share my results now with the community.

Using the Code

It all starts with the 'ManagementClass' which is using a system defined class name (e.g. "Win32_PhysicalMemory"). This class name is used to create a management path of the requested data. The system contains a huge amount of class names, all listed here.

C#
ManagementClass MgmtClass = new ManagementClass(new ManagementPath("Win32_PhysicalMemory"));

The information items in this class cannot be read out directly; we must create a collection of objects:

C#
ManagementObjectCollection MOC = MgmtClass.GetInstances();

A management class may have multiple instances, e.g., when reading the information about the memory banks. The motherboard of my computer system is equipped with RAM in all four memory banks, so I will get four instances of the management class "Win32_PhysicalMemory" and the same number of items in the management objects collection.

A complete list of all information objects of all memory management instances is now obtained with two nested enumerations. The outer enumeration covers the instances of the management class; the inner enumeration returns all information items with their internal names and values. While the outer enumeration must be done using an enumerator, we can do the inner enumeration with a foreach loop:

C#
ManagementObjectCollection.ManagementObjectEnumerator MOE = MOC.GetEnumerator();
while (MOE.MoveNext()) {
  ManagementBaseObject mbo = MOE.Current;
  PropertyDataCollection pdc = mbo.Properties;
  foreach (PropertyData pd in pdc) {
    object value = pd.Value;
    if (value == null) continue;
    // ----- convert value object to a string
    // ----- attention: values may appear as arrays, boxed in a single object
    //  .....
  }
}

That's it. The complete code of this hardware features enumeration can be found in the attached ZIP file which contains all VS2015 files to compile and run the code.

Note: The sources contain three extra blocks of code which I wrote to obtain more specialized computer information. These blocks have been commented out since they are not required for the demo application.

History

  • 2016-10-25: Initial edition

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Product Manager
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThe sample use case... Pin
dandy7225-Oct-16 6:17
dandy7225-Oct-16 6:17 

performance would suffer significantly if I had to load the files again and again, so I started to write a caching application for my JPG files. This application should use as much memory space as possible, without (if possible) forcing the system to use the virtual space on the harddisk


That's a sensible thing to try to achieve, but obtaining the amount of physical memory is really not going to present a complete picture, isn't it? If you detect the system has 8GB of physical memory, you're going to create a cache that's slightly under 8GB, and assume you're going to be ok and that'll be enough to prevent the system from paging? That might work if you know yours is the only application that's running on a system, but that's far from being representative of the real world. If I'm running another app that's chewing 6GB, whatever you've obtained about the amount of physical memory isn't going to help you much in trying to make your app avoid paging, because it'll already have started long before you fill even half of your cache.

To achieve what you're claiming to try to achieve with this sample, I'd think it would be a lot more effective to look at the current state of the (system-wide) paged/virtual memory, as you've stated yourself. However the code you've posted here doesn't help much with this.

There's nothing wrong with the code you've posted in and of itself - I'm just saying I'd restate its purpose, perhaps with another example. For example, if you want to know whether the machine has some free memory slots you can use for a potential upgrade, or show detailed characteristics about each memory stick that is present - that information is also in WMI - in fact some of it is in the same table you're already querying.

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.