Thursday, 14 February 2013

Getting BIOS Information using C#


Win32_BIOS: The Win32_BIOS WMI class represents the attributes of the computer system’s basic input/output services (BIOS) that are installed on a computer
Name
Data type
Description
BiosCharacteristics
uint16
Array of BIOS characteristics supported by the system as defined by the System Management BIOS Reference Specification
BIOSVersion
string
Array of the complete system BIOS information
BuildNumber
string
Internal identifier for this compilation of this software element
Caption
string
Short description of the object-a one line string
CurrentLanguage
string
Name of the current BIOS language
Description
string
Description of the object
InstallableLanguages
uint16
Number of languages available for installation on this system
InstallDate
datetime
Date and time the object was installed
LanguageEdition
string
Language edition of this software element
Manufacturer
string
Manufacturer of this software element
Name
string
Name used to identify this software element
PrimaryBIOS
boolean
If TRUE, this is the primary BIOS of the computer system
ReleaseDate
datetime
Release date of the Windows BIOS in the Coordinated Universal Time (UTC) format of YYYYMMDDHHMMSS.MMMMMM(+ -)OOO
SerialNumber
string
Assigned serial number of the software element
SMBIOSBIOSVersion
string
BIOS version as reported by SMBIOS
SMBIOSMajorVersion
uint16
Major SMBIOS version number
SMBIOSMinorVersion
uint16
Minor SMBIOS version number
SMBIOSPresent
boolean
If TRUE, the SMBIOS is available on this computer system
SoftwareElementID
string
Identifier for this software element
SoftwareElementState
uint16
State of a software element
Status
string
Current status of the object
TargetOperatingSystem
uint16
Target operating system of the owning software element
Version
string
Version of the BIOS

BiosCharacteristics Properties:
Value
Meaning
0
Reserved
1
Reserved
2
Unknown
3
BIOS Characteristics Not Supported
4
ISA is supported
5
MCA is supported
6
EISA is supported
7
PCI is supported
8
PC Card (PCMCIA) is supported
9
Plug and Play is supported
10
APM is supported
11
BIOS is Upgradable (Flash)
12
BIOS shadowing is allowed
13
VL-VESA is supported
14
ESCD support is available
15
Boot from CS is supported
16
Selectable Boot is supported
17
BIOS ROM is socketed
18
Boot From PC Card (PCMCIA) is supported
19
EDD (Enhanced Disk Drive) Specification is supported
20
Int 13h – Japanese Floppy for NEC 9800 1.2mb (3.5, 1k Bytes/Sector, 360 RPM) is supported
21
Int 13h – Japanese Floppy for Toshiba 1.2mb (3.5, 360 RPM) is supported
22
Int 13h – 5.25/360KB Floppy Services are supported
23
Int 13h – 5.25/1.2 MB Floppy Services are supported
24
Int 13h – 3.5/720KB Floppy Services are supported
25
Int 13h – 3.5/2.88 MB Floppy Services are supported
26
Int 5h, Print Screen Service is supported
27
Int 9h, 8042 Keyboard services are supported
28
Int 14h, Serial Services are supported
29
Int 17h, printer services are supported
30
Int 10h, CGA/Mono Video Services are supported
31
NEC PC-98
32
ACPI is supported
33
USB Legacy is supported
34
AGP is supported
35
I2O boot is supported
36
LS-120 boot is supported
37
ATAPI ZIP Drive boot is supported
38
1394 boot is supported
39
Smart Battery is supported
40:47
Reserved for BIOS vendor
48:63
Reserved for system vendor

SoftwareElementState Properties:
Value
Meaning
0
Deployable
1
Installable
2
Executable
3
Running

Status Properties:
·         OK
·         Error
·         Degraded
·         Unknown
·         Pred Fail
·         Starting
·         Stopping
·         Service
·         Stressed
·         NonRecover
·         No Contact
·         Lost Comm
TargetOperatingSystem Properties:
Value
Meaning
0
Unknown
1
Other
2
MACOS
3
ATTUNIX
4
DGUX
5
DECNT
6
Digital Unix
7
OpenVMS
8
HPUX
9
AIX
10
MVS
11
OS400
12
OS/2
13
JavaVM
14
MSDOS
15
WIN3x
16
WIN95
17
WIN98
18
WINNT
19
WINCE
20
NCR3000
21
NetWare
22
OSF
23
DC/OS
24
Reliant UNIX
25
SCO UnixWare
26
SCO OpenServer
27
Sequent
28
IRIX
29
Solaris
30
SunOS
31
U6000
32
ASERIES
33
TandemNSK
34
TandemNT
35
BS2000
36
LINUX
37
Lynx
38
XENIX
39
VM/ESA
40
Interactive UNIX
41
BSDUNIX
42
FreeBSD
43
NetBSD
44
GNU Hurd
45
OS9
46
MACH Kernel
47
Inferno
48
QNX
49
EPOC
50
IxWorks
51
VxWorks
52
MiNT
53
BeOS
54
HP MPE
55
NextStep
56
PalmPilot
57
Rhapsody
58
Windows 2000
59
Dedicated
60
VSE
61
TPF

Step 1: Create New Project (Console Application)
Step 2: Add “System.Management” namespace.
Step 3: Add a new “Class” file (say, BIOSInfo.cs)
Step 4: Write the following code for “BIOSInfo.cs”
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace BIOS
{
    static public class BIOSInfo
    {
        private static ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BIOS");

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

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

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

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

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

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

        static public int InstallableLanguages
        {
            get
            {
                try
                {
                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        return int.Parse(queryObj["InstallableLanguages"].ToString());
                    }
                    return 0;
                }
                catch (Exception e)
                {
                    return 0;
                }
            }
        }

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

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

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

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

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

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

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

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

        static public int SMBIOSMajorVersion
        {
            get
            {
                try
                {
                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        return int.Parse(queryObj["SMBIOSMajorVersion"].ToString());
                    }
                    return 0;
                }
                catch (Exception e)
                {
                    return 0;
                }
            }
        }

        static public int SMBIOSMinorVersion
        {
            get
            {
                try
                {
                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        return int.Parse(queryObj["SMBIOSMinorVersion"].ToString());
                    }
                    return 0;
                }
                catch (Exception e)
                {
                    return 0;
                }
            }
        }

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

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

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

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

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

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

        private static string GetBiosCharacteristics(int charac)
        {
            switch (charac)
            {
                case 0: return "Reserved";
                case 1: return "Reserved";
                case 2: return "Unknown";
                case 3: return "BIOS Characteristics Not Supported";
                case 4: return "ISA is supported";
                case 5: return "MCA is supported";
                case 6: return "EISA is supported";
                case 7: return "PCI is supported";
                case 8: return "PC Card (PCMCIA) is supported";
                case 9: return "Plug and Play is supported";
                case 10: return "APM is supported";
                case 11: return "BIOS is Upgradable (Flash)";
                case 12: return "BIOS shadowing is allowed";
                case 13: return "VL-VESA is supported";
                case 14: return "ESCD support is available";
                case 15: return "Boot from CS is supported";
                case 16: return "Selectable Boot is supported";
                case 17: return "BIOS ROM is socketed";
                case 18: return "Boot From PC Card (PCMCIA) is supported";
                case 19: return "EDD (Enhanced Disk Drive) Specification is supported";
                case 20: return "Int 13h - Japanese Floppy for NEC 9800 1.2mb (3.5, 1k Bytes/Sector, 360RPM) is supported";
                case 21: return "Int 13h - Japanese Floppy for Toshiba 1.2mb (3.5, 360RPM) is supported";
                case 22: return "Int 13h - 5.25/360KB Floppy Services are supported";
                case 23: return "Int 13h - 5.25/1.2MB Floppy Services are supported";
                case 24: return "Int 13h - 3.5/720KB Floppy Services are supported";
                case 25: return "Int 13h - 3.5/2.88MB Floppy Services are supported";
                case 26: return "Int 5h, Print Screen Service is supported";
                case 27: return "Int 9h, 8042 Keyboard Services are supported";
                case 28: return "Int 14h, Serial Services are supported";
                case 29: return "Int 17h, Printer Services are supported";
                case 30: return "Int 10h, CGA/Mono Video Services are supported";
                case 31: return "NEC PC-98";
                case 32: return "ACPI is supported";
                case 33: return "USB Legacy is supported";
                case 34: return "AGP is supported";
                case 35: return "I2O boot is supported";
                case 36: return "LS-120 boot is supported";
                case 37: return "ATAPI ZIP Drive boot is supported";
                case 38: return "1394 boot is supported";
                case 39: return "Smart Battery is supported";
                case 40: return "Reserved for BIOS vendor";
                case 41: return "Reserved for BIOS vendor";
                case 42: return "Reserved for BIOS vendor";
                case 43: return "Reserved for BIOS vendor";
                case 44: return "Reserved for BIOS vendor";
                case 45: return "Reserved for BIOS vendor";
                case 46: return "Reserved for BIOS vendor";
                case 47: return "Reserved for BIOS vendor";
                case 48: return "Reserved for system vendor";
                case 49: return "Reserved for system vendor";
                case 50: return "Reserved for system vendor";
                case 51: return "Reserved for system vendor";
                case 52: return "Reserved for system vendor";
                case 53: return "Reserved for system vendor";
                case 54: return "Reserved for system vendor";
                case 55: return "Reserved for system vendor";
                case 56: return "Reserved for system vendor";
                case 57: return "Reserved for system vendor";
                case 58: return "Reserved for system vendor";
                case 59: return "Reserved for system vendor";
                case 60: return "Reserved for system vendor";
                case 61: return "Reserved for system vendor";
                case 62: return "Reserved for system vendor";
                case 63: return "Reserved for system vendor";
                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;
        }

        private static string GetSoftwareElementState(int ses)
        {
            switch (ses)
            {
                case 0: return "Deployable";
                case 1: return "Installable";
                case 2: return "Executable";
                case 3: return "Running";
                default: return "Unknown";
            }
        }

        private static string GetTargetOperatingSystem(int tos)
        {
            switch (tos)
            {
                case 0: return "Unknown";
                case 1: return "Other";
                case 2: return "MACOS";
                case 3: return "ATTUNIX";
                case 4: return "DGUX";
                case 5: return "DECNT";
                case 6: return "Digital Unix";
                case 7: return "OpenVMS";
                case 8: return "HPUX";
                case 9: return "AIX";
                case 10: return "MVS";
                case 11: return "OS400";
                case 12: return "OS/2";
                case 13: return "JavaVM";
                case 14: return "MSDOS";
                case 15: return "WIN3x";
                case 16: return "WIN95";
                case 17: return "WIN98";
                case 18: return "WINNT";
                case 19: return "WINCE";
                case 20: return "NCR3000";
                case 21: return "NetWare";
                case 22: return "OSF";
                case 23: return "DC/OS";
                case 24: return "Reliant UNIX";
                case 25: return "SCO UnixWare";
                case 26: return "SCO OpenServer";
                case 27: return "Sequent";
                case 28: return "IRIX";
                case 29: return "Solaris";
                case 30: return "SunOS";
                case 31: return "U6000";
                case 32: return "ASERIES";
                case 33: return "TandemNSK";
                case 34: return "TandemNT";
                case 35: return "BS2000";
                case 36: return "LINUX";
                case 37: return "Lynx";
                case 38: return "XENIX";
                case 39: return "VM/ESA";
                case 40: return "Interactive UNIX";
                case 41: return "BSDUNIX";
                case 42: return "FreeBSD";
                case 43: return "NetBSD";
                case 44: return "GNU Hurd";
                case 45: return "OS9";
                case 46: return "MACH Kernel";
                case 47: return "Inferno";
                case 48: return "QNX";
                case 49: return "EPOC";
                case 50: return "IxWorks";
                case 51: return "VxWorks";
                case 52: return "MiNT";
                case 53: return "BeOS";
                case 54: return "HP MPE";
                case 55: return "NextStep";
                case 56: return "PalmPilot";
                case 57: return "Rhapsody";
                case 58: return "Windows 2000";
                case 59: return "Dedicated";
                case 60: return "VSE";
                case 61: return "TPF";
                default: return "Unknown";
            }
        }
    }
}

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

namespace BIOS
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("BIOS Properties:");
            Console.WriteLine("-----------------------------------------------------------------------------");
            Console.WriteLine("-----------------------------------------------------------------------------");
            Console.WriteLine("BiosCharacteristics: " + BIOSInfo.BiosCharacteristics);
            Console.WriteLine("BIOSVersion: " + BIOSInfo.BIOSVersion);
            Console.WriteLine("BuildNumber: " + BIOSInfo.BuildNumber);
            Console.WriteLine("Caption: " + BIOSInfo.Caption);
            Console.WriteLine("CurrentLanguage: " + BIOSInfo.CurrentLanguage);
            Console.WriteLine("Description: " + BIOSInfo.Description);
            Console.WriteLine("InstallableLanguages: " + BIOSInfo.InstallableLanguages);
            Console.WriteLine("InstallDate: " + BIOSInfo.InstallDate);
            Console.WriteLine("LanguageEdition: " + BIOSInfo.LanguageEdition);
            Console.WriteLine("Manufacturer: " + BIOSInfo.Manufacturer);
            Console.WriteLine("Name: " + BIOSInfo.Name);
            Console.WriteLine("PrimaryBIOS: " + BIOSInfo.PrimaryBIOS);
            Console.WriteLine("ReleaseDate: " + BIOSInfo.ReleaseDate);
            Console.WriteLine("SerialNumber: " + BIOSInfo.SerialNumber);
            Console.WriteLine("SMBIOSBIOSVersion: " + BIOSInfo.SMBIOSBIOSVersion);
            Console.WriteLine("SMBIOSMajorVersion: " + BIOSInfo.SMBIOSMajorVersion);
            Console.WriteLine("SMBIOSMinorVersion: " + BIOSInfo.SMBIOSMinorVersion);
            Console.WriteLine("SMBIOSPresent: " + BIOSInfo.SMBIOSPresent);
            Console.WriteLine("SoftwareElementID: " + BIOSInfo.SoftwareElementID);
            Console.WriteLine("SoftwareElementState: " + BIOSInfo.SoftwareElementState);
            Console.WriteLine("Status: " + BIOSInfo.Status);
            Console.WriteLine("TargetOperatingSystem: " + BIOSInfo.TargetOperatingSystem);
            Console.WriteLine("Version: " + BIOSInfo.Version);
            Console.ReadLine();
        }
    }
}



1 comment: