How to Retrieve Machine Chassis Information in C#


 When developing Windows applications, you may sometimes need to know what type of chassis (like desktop, laptop, or server) your application is running on. This information can be particularly useful when configuring hardware-specific settings or optimizing application performance for different device types. In this blog post, we’ll explore how to retrieve machine chassis information in C# using Windows Management Instrumentation (WMI).

Let’s walk through a code snippet that identifies the chassis type of the machine your application is running on.


Understanding Chassis Types

Chassis types refer to the physical configuration of a computer, such as a desktop, laptop, or rack-mounted server. In C#, we can define an enumeration (enum) to represent these types. Here’s a list of common chassis types:

public enum ChassisTypes
{
    Other = 1,
    Unknown,
    Desktop,
    LowProfileDesktop,
    PizzaBox,
    MiniTower,
    Tower,
    Portable,
    Laptop,
    Notebook,
    Handheld,
    DockingStation,
    AllInOne,
    SubNotebook,
    SpaceSaving,
    LunchBox,
    MainSystemChassis,
    ExpansionChassis,
    SubChassis,
    BusExpansionChassis,
    PeripheralChassis,
    StorageChassis,
    RackMountChassis,
    SealedCasePC
}

Each chassis type is assigned an integer value. This allows us to map numerical identifiers retrieved from the system to specific chassis descriptions.

Retrieving the Current Chassis Type

To access the chassis type, we use WMI (Windows Management Instrumentation) to query the Win32_SystemEnclosure class, which contains information about the computer chassis. The ChassisTypes property in this class returns an array of integers representing possible chassis types.

Here’s the full code to retrieve the current chassis type:

using System;
using System.Management;

public static class ChassisInfo
{
    public static ChassisTypes GetCurrentChassisType()
    {
        ManagementClass systemEnclosures = new ManagementClass("Win32_SystemEnclosure");
        
        foreach (ManagementObject obj in systemEnclosures.GetInstances())
        {
            foreach (int i in (UInt16[])(obj["ChassisTypes"]))
            {
                if (i > 0 && i < 25)
                {
                    return (ChassisTypes)i;
                }
            }
        }
        
        return ChassisTypes.Unknown;
    }
}


Let’s break down the code step-by-step:

  1. ManagementClass Initialization: We start by initializing a ManagementClass object for the Win32_SystemEnclosure WMI class.
  2. Query Instances: We retrieve all instances of Win32_SystemEnclosure. Although there’s typically only one instance per machine, this approach handles multiple instances if necessary.
  3. Retrieve Chassis Types: For each instance, we read the ChassisTypes property, which is an array of integers (of type UInt16[]). Each integer corresponds to a chassis type.
  4. Map Integer to Enum: We check if the integer is within the range of defined values in our ChassisTypes enum (1 to 24). If valid, we cast the integer to a ChassisTypes value and return it.
  5. Default to Unknown: If no valid chassis type is found, we return ChassisTypes.Unknown.

Example Usage

To get the chassis type and display it in your application, call GetCurrentChassisType() like so:

public class Program
{
    public static void Main()
    {
        ChassisTypes chassisType = ChassisInfo.GetCurrentChassisType();
        Console.WriteLine("Current Chassis Type: " + chassisType);
    }
}

When run, this code will output the chassis type of the machine, for example:
Current Chassis Type: Laptop


Why Use WMI for Chassis Information?

WMI provides a standardized way to retrieve system information across Windows devices. By leveraging WMI, we can query detailed hardware information without requiring direct access to device-specific APIs, making the code more portable and easier to maintain.

Use Cases for Chassis Information

Retrieving chassis type information can be useful in various scenarios, such as:

  • Tailoring Software for Different Devices: You might have specific configurations for desktops, laptops, and servers to optimize performance and usability.
  • Security and Compliance: Knowing the device type can help enforce different security policies, such as stricter protocols on portable devices like laptops.
  • Device-Specific UI Adjustments: On handheld or laptop devices, your application may need a more compact UI, while desktops and servers can afford more screen space.

Conclusion

With the code shown above, you can easily retrieve the chassis type of a machine in C#. This can be a powerful tool in applications that need to adapt to different hardware types. By using WMI and enums in C#, your application can gain insights into the device it’s running on, leading to more adaptable and efficient solutions.

You can get the complete on Github


Previous Post Next Post

نموذج الاتصال