Tuesday, September 27, 2011

How to get System Information (Hardware and Software) in C# (WPF)?


Sometimes we need get some machine specific (hardware) information such as ProcessorID, Motherboard Name, Hard Disk Serial Number, Installed Printers, Installed Memory etc. We can get all these information using Dotnet. Dotnet provides System.Management namespace which is used to get all Hardware and Software information of your machine.

System.Management provides class called ManagementObjectSearcher which is used to query hardware and software information from machine based on key. We can get ManagementObjectCollection by calling get method of ManagementObjectSearcher class. Now get ManagementObject by iterating ManagementObjectCollection instance. ManagementObject class provides properties and using this property we can iterate all the PropertyData available under ManagementObject. 

Let’s have a look on below example created with C# and WPF.

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0.7*" />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="35"/>
        <RowDefinition />
    </Grid.RowDefinitions>
    <Label Content="Select Hardware:"
        VerticalAlignment="Center"
        Margin="5"
        Grid.Row="0" Grid.Column="0"/>
    <ComboBox Name="SysInfoComboBox"
        Grid.Row="0" Grid.Column="1"
        Margin="5"
        SelectionChanged="SysInfoComboBox_SelectionChanged"/>
    <ListBox Name="SystemInfoListBox"
        Grid.Row="1" Grid.ColumnSpan="2" Margin="5"
        ScrollViewer.HorizontalScrollBarVisibility="Auto"
        ScrollViewer.VerticalScrollBarVisibility="Auto"/>
</Grid>

void SystemInfo_Loaded(object sender, RoutedEventArgs e)
{
    FillComboBox();
}
void FillComboBox()
{
    SysInfoComboBox.Items.Add("Select a Value");
    SysInfoComboBox.Items.Add("OperatingSystem");
    SysInfoComboBox.Items.Add("DiskDrive");
    SysInfoComboBox.Items.Add("MotherboardDevice");
    SysInfoComboBox.Items.Add("Processor");
    SysInfoComboBox.Items.Add("Printer");
    SysInfoComboBox.Items.Add("ComputerSystem");

    SysInfoComboBox.SelectedIndex = 0;
}
void FillListBox(string value)
{
    List<string> items = new List<string>();
    ManagementObjectSearcher mSearchObj =
         new ManagementObjectSearcher("SELECT * FROM " + value);
    ManagementObjectCollection objCollection = mSearchObj.Get();

    foreach (ManagementObject mObject in objCollection)
    {
        foreach (PropertyData property in mObject.Properties)
        {
            items.Add(string.Format("{0}:- {1}", property.Name, property.Value));
        }
    }
    SystemInfoListBox.ItemsSource = items;
}
private void SysInfoComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (SysInfoComboBox.SelectedIndex > 0)
    {
        SystemInfoListBox.ClearValue(ListBox.ItemsSourceProperty);
        FillListBox("Win32_" + SysInfoComboBox.SelectedItem.ToString());
    }
}






In above example, I have added ComboBox and ListBox. ComboBox contains the key name of hardware of which we need to get information. Based on the value selected in Combobox the Listbox will get populated with information on selection changed event of combobox.

ManagementObjectSearcher mSearchObj =
         new ManagementObjectSearcher("SELECT * FROM " + value);
ManagementObjectCollection objCollection = mSearchObj.Get();

As per above code snippet, mSearchObj.Get() method will get ManagementObjectCollection based on passed value to query.

    foreach (ManagementObject mObject in objCollection)
    {
        foreach (PropertyData property in mObject.Properties)
        {
            items.Add(string.Format("{0}:- {1}", property.Name, property.Value));
        }
    }

The above nested foreach loop will iterate each ManagementObject available in objcollection and each PropertyData available in ManagementObject’s Properties. Inside nested loop I am adding Property Name and value into List collection of string and assigning to Listbox’s datasource.

Hope you liked this post. Please leave your comments and feedback in comments section of this post.

See also - 

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hello Friend,
    this is really nice post , but I have some more questions ..... how we can Check for each location if there are updates available means on client side

    ReplyDelete