Thursday, 14 February 2013

Getting Motherboard Information using C#



Win32_BaseBoard: The Win32_BaseBoard WMI class represents a baseboard, which is also known as a motherboard or system board.
Win32_MotherboardDevice: The Win32_MotherboardDevice WMI class represents a device that contains the central components of the Windows computer system.
Name
WMI Class
Data type
Description
Availability
Win32_MotherboardDevice
Uint16
Availability and status of the device
HostingBoard
Win32_BaseBoard
Boolean
If TRUE, the card is a motherboard or a baseboard in a chasis
InstallDate
Win32_BaseBoard
Datetime
Date and time the object was installed
Manufacturer
Win32_BaseBoard
String
Name of the organization responsible for producing the physical element
Model
Win32_BaseBoard
String
Name by which the physical element is known
PartNumber
Win32_BaseBoard
String
Part number assigned by the organization responsible for producing or manufacturing the physical element
PNPDeviceID
Win32_MotherboardDevice
String
Windows Plug and Play device identifier of the logical device
PrimaryBusType
Win32_MotherboardDevice
String
Primary bus type of the motherboard
Product
Win32_BaseBoard
String
Baseboard part number defined by the manufacturer
Removable
Win32_BaseBoard
Boolean
If TRUE, a package is removable
Replaceable
Win32_BaseBoard
Boolean
If TRUE, a package is replaceable
RevisionNumber
Win32_MotherboardDevice
String
Revision number of the motherboard
SecondaryBusType
Win32_MotherboardDevice
String
Secondary bus type of the motherboard
SerialNumber
Win32_BaseBoard
String
Manufacturer-allocated number used to identify the physical element
Status
Win32_BaseBoard
String
Current status of the object
SystemName
Win32_MotherboardDevice
String
Name of the scoping system
Version
Win32_BaseBoard
String
Version of the physical element

Availability Properties:
Value
Meaning
1
Other
2
Unknown
3
Running or Full Power
4
Warning
5
In Test
6
Not Applicable
7
Power Off
8
Off Line
9
Off Duty
10
Degraded
11
Not Installed
12
Install Error
13
Power Save – Unknown
14
Power Save – Low Power Mode
15
Power Save – Standby
16
Power Cycle
17
Power Save – Warning

Status Properties:
·         OK
·         Error
·         Degraded
·         Unknown
·         Starting
·         Pred Fail
·         Stopping
·         Service
·         Stressed
·         NonRecover
·         No Contact
·         Lost Comm
Step 1: Create New Project (Console Application)
Step 2: Add “System.Management” namespace.
Step 3: Add a new “Class” file (say, MotherboardInfo.cs)
Step 4: Write the following code for “MotherboardInfo.cs”
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace Motherboard
{
    static public class MotherboardInfo
    {
        private static ManagementObjectSearcher baseboardSearcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BaseBoard");
        private static ManagementObjectSearcher motherboardSearcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_MotherboardDevice");

        static public string Availability
        {
            get
            {
                try
                {
                    foreach (ManagementObject queryObj in motherboardSearcher.Get())
                    {
                        return GetAvailability(int.Parse(queryObj["Availability"].ToString()));
                    }
                    return "";
                }
                catch (Exception e)
                {
                    return "";
                }
            }
        }

        static public bool HostingBoard
        {
            get
            {
                try
                {
                    foreach (ManagementObject queryObj in baseboardSearcher.Get())
                    {
                        if (queryObj["HostingBoard"].ToString() == "True")
                            return true;
                        else
                            return false;
                    }
                    return false;
                }
                catch (Exception e)
                {
                    return false;
                }
            }
        }

        static public string InstallDate
        {
            get
            {
                try
                {
                    foreach (ManagementObject queryObj in baseboardSearcher.Get())
                    {
                        return ConvertToDateTime(queryObj["InstallDate"].ToString());
                    }
                    return "";
                }
                catch (Exception e)
                {
                    return "";
                }
            }
        }

        static public string Manufacturer
        {
            get
            {
                try
                {
                    foreach (ManagementObject queryObj in baseboardSearcher.Get())
                    {
                        return queryObj["Manufacturer"].ToString();
                    }
                    return "";
                }
                catch (Exception e)
                {
                    return "";
                }
            }
        }

        static public string Model
        {
            get
            {
                try
                {
                    foreach (ManagementObject queryObj in baseboardSearcher.Get())
                    {
                        return queryObj["Model"].ToString();
                    }
                    return "";
                }
                catch (Exception e)
                {
                    return "";
                }
            }
        }

        static public string PartNumber
        {
            get
            {
                try
                {
                    foreach (ManagementObject queryObj in baseboardSearcher.Get())
                    {
                        return queryObj["PartNumber"].ToString();
                    }
                    return "";
                }
                catch (Exception e)
                {
                    return "";
                }
            }
        }

        static public string PNPDeviceID
        {
            get
            {
                try
                {
                    foreach (ManagementObject queryObj in motherboardSearcher.Get())
                    {
                        return queryObj["PNPDeviceID"].ToString();
                    }
                    return "";
                }
                catch (Exception e)
                {
                    return "";
                }
            }
        }

        static public string PrimaryBusType
        {
            get
            {
                try
                {
                    foreach (ManagementObject queryObj in motherboardSearcher.Get())
                    {
                        return queryObj["PrimaryBusType"].ToString();
                    }
                    return "";
                }
                catch (Exception e)
                {
                    return "";
                }
            }
        }

        static public string Product
        {
            get
            {
                try
                {
                    foreach (ManagementObject queryObj in baseboardSearcher.Get())
                    {
                        return queryObj["Product"].ToString();
                    }
                    return "";
                }
                catch (Exception e)
                {
                    return "";
                }
            }
        }

        static public bool Removable
        {
            get
            {
                try
                {
                    foreach (ManagementObject queryObj in baseboardSearcher.Get())
                    {
                        if (queryObj["Removable"].ToString() == "True")
                            return true;
                        else
                            return false;
                    }
                    return false;
                }
                catch (Exception e)
                {
                    return false;
                }
            }
        }

        static public bool Replaceable
        {
            get
            {
                try
                {
                    foreach (ManagementObject queryObj in baseboardSearcher.Get())
                    {
                        if (queryObj["Replaceable"].ToString() == "True")
                            return true;
                        else
                            return false;
                    }
                    return false;
                }
                catch (Exception e)
                {
                    return false;
                }
            }
        }

        static public string RevisionNumber
        {
            get
            {
                try
                {
                    foreach (ManagementObject queryObj in motherboardSearcher.Get())
                    {
                        return queryObj["RevisionNumber"].ToString();
                    }
                    return "";
                }
                catch (Exception e)
                {
                    return "";
                }
            }
        }

        static public string SecondaryBusType
        {
            get
            {
                try
                {
                    foreach (ManagementObject queryObj in motherboardSearcher.Get())
                    {
                        return queryObj["SecondaryBusType"].ToString();
                    }
                    return "";
                }
                catch (Exception e)
                {
                    return "";
                }
            }
        }

        static public string SerialNumber
        {
            get
            {
                try
                {
                    foreach (ManagementObject queryObj in baseboardSearcher.Get())
                    {
                        return queryObj["SerialNumber"].ToString();
                    }
                    return "";
                }
                catch (Exception e)
                {
                    return "";
                }
            }
        }

        static public string Status
        {
            get
            {
                try
                {
                    foreach (ManagementObject querObj in baseboardSearcher.Get())
                    {
                        return querObj["Status"].ToString();
                    }
                    return "";
                }
                catch (Exception e)
                {
                    return "";
                }
            }
        }

        static public string SystemName
        {
            get
            {
                try
                {
                    foreach (ManagementObject queryObj in motherboardSearcher.Get())
                    {
                        return queryObj["SystemName"].ToString();
                    }
                    return "";
                }
                catch (Exception e)
                {
                    return "";
                }
            }
        }

        static public string Version
        {
            get
            {
                try
                {
                    foreach (ManagementObject queryObj in baseboardSearcher.Get())
                    {
                        return queryObj["Version"].ToString();
                    }
                    return "";
                }
                catch (Exception e)
                {
                    return "";
                }
            }
        }

        private static string GetAvailability(int availability)
        {
            switch (availability)
            {
                case 1: return "Other";
                case 2: return "Unknown";
                case 3: return "Running or Full Power";
                case 4: return "Warning";
                case 5: return "In Test";
                case 6: return "Not Applicable";
                case 7: return "Power Off";
                case 8: return "Off Line";
                case 9: return "Off Duty";
                case 10: return "Degraded";
                case 11: return "Not Installed";
                case 12: return "Install Error";
                case 13: return "Power Save - Unknown";
                case 14: return "Power Save - Low Power Mode";
                case 15: return "Power Save - Standby";
                case 16: return "Power Cycle";
                case 17: return "Power Save - Warning";
                default: return "Unknown";
            }
        }

        private static string ConvertToDateTime(string unconvertedTime)
        {
            string convertedTime = "";
            int year = int.Parse(unconvertedTime.Substring(0, 4));
            int month = int.Parse(unconvertedTime.Substring(4, 2));
            int date = int.Parse(unconvertedTime.Substring(6, 2));
            int hours = int.Parse(unconvertedTime.Substring(8, 2));
            int minutes = int.Parse(unconvertedTime.Substring(10, 2));
            int seconds = int.Parse(unconvertedTime.Substring(12, 2));
            string meridian = "AM";
            if (hours > 12)
            {
                hours -= 12;
                meridian = "PM";
            }
            convertedTime = date.ToString() + "/" + month.ToString() + "/" + year.ToString() + " " +
            hours.ToString() + ":" + minutes.ToString() + ":" + seconds.ToString() + " " + meridian;
            return convertedTime;
        }
    }
}

Step 5: Write the following code for “Program.cs”
using System;
using System.Collections.Generic;
using System.Text;

namespace Motherboard
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Motherboard Properties:");
            Console.WriteLine("-----------------------------------------------------------------------------");
            Console.WriteLine("-----------------------------------------------------------------------------");
            Console.WriteLine("Availability: " + MotherboardInfo.Availability);
            Console.WriteLine("HostingBoard: " + MotherboardInfo.HostingBoard);
            Console.WriteLine("InstallDate: " + MotherboardInfo.InstallDate);
            Console.WriteLine("Manufacturer: " + MotherboardInfo.Manufacturer);
            Console.WriteLine("Model: " + MotherboardInfo.Model);
            Console.WriteLine("PartNumber: " + MotherboardInfo.PartNumber);
            Console.WriteLine("PNPDeviceID: " + MotherboardInfo.PNPDeviceID);
            Console.WriteLine("PrimaryBusType: " + MotherboardInfo.PrimaryBusType);
            Console.WriteLine("Product: " + MotherboardInfo.Product);
            Console.WriteLine("Removable: " + MotherboardInfo.Removable);
            Console.WriteLine("Replaceable: " + MotherboardInfo.Replaceable);
            Console.WriteLine("RevisionNumber: " + MotherboardInfo.RevisionNumber);
            Console.WriteLine("SecondaryBusType: " + MotherboardInfo.SecondaryBusType);
            Console.WriteLine("SerialNumber: " + MotherboardInfo.SerialNumber);
            Console.WriteLine("Status: " + MotherboardInfo.Status);
            Console.WriteLine("SystemName: " + MotherboardInfo.SystemName);
            Console.WriteLine("Version: " + MotherboardInfo.Version);
            Console.ReadLine();
        }
    }
}


2 comments: