Get System Information in C#

In C#, we can get system information like Operation System , Processor Architecture and Drive Details using the built-in class Environment and we can also extract information through WMI service.

Get System Information using Environment

The below C# function get system information using Environment method.

public static void ShowSystemInformation()
{
    StringBuilder systemInfo = new StringBuilder(string.Empty);

    systemInfo.AppendFormat("Operation System:  {0}n", Environment.OSVersion);
    systemInfo.AppendFormat("Processor Architecture:  {0}n", Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"));
    systemInfo.AppendFormat("Processor Model:  {0}n", Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER"));
    systemInfo.AppendFormat("Processor Level:  {0}n", Environment.GetEnvironmentVariable("PROCESSOR_LEVEL"));
    systemInfo.AppendFormat("SystemDirectory:  {0}n", Environment.SystemDirectory);
    systemInfo.AppendFormat("ProcessorCount:  {0}n", Environment.ProcessorCount);
    systemInfo.AppendFormat("UserDomainName:  {0}n", Environment.UserDomainName);
    systemInfo.AppendFormat("UserName: {0}n", Environment.UserName);
    //Drives
    systemInfo.AppendFormat("LogicalDrives:n");
    foreach (System.IO.DriveInfo DriveInfo1 in System.IO.DriveInfo.GetDrives())
    {
        try
        {
            systemInfo.AppendFormat("t Drive: {0}ntt VolumeLabel: " +
                "{1}ntt DriveType: {2}ntt DriveFormat: {3}ntt " +
                "TotalSize: {4}ntt AvailableFreeSpace: {5}n",
                DriveInfo1.Name, DriveInfo1.VolumeLabel, DriveInfo1.DriveType,
                DriveInfo1.DriveFormat, DriveInfo1.TotalSize, DriveInfo1.AvailableFreeSpace);
        }
        catch
        {
        }
    }
    systemInfo.AppendFormat("Version:  {0}", Environment.Version);
    Console.WriteLine(systemInfo);
    Console.ReadKey();
}

Output:

Operation System:  Microsoft Windows NT 6.1.7601 Service Pack 1
Processor Architecture:  AMD64
Processor Model:  Intel64 Family 6 Model 42 Stepping 7, GenuineIntel
Processor Level:  6
SystemDirectory:  C:Windowssystem32
ProcessorCount:  2
UserDomainName:  TESTDomain
UserName: Administrator
LogicalDrives:
  Drive: C:
   VolumeLabel: 
   DriveType: Fixed
   DriveFormat: NTFS
   TotalSize: 43240292352
   AvailableFreeSpace: 10865721344
  Drive: D:
   VolumeLabel: Local Disk
   DriveType: Fixed
   DriveFormat: NTFS
   TotalSize: 42183159808
   AvailableFreeSpace: 12267360256
  Drive: E:
   VolumeLabel: VBOXADDITIONS_4.
   DriveType: CDRom
   DriveFormat: CDFS
   TotalSize: 64770048
   AvailableFreeSpace: 0
  Drive: F:
   VolumeLabel: VBOX_Downloads
   DriveType: Network
   DriveFormat: VBoxSharedFolderFS
   TotalSize: 729626107904
   AvailableFreeSpace: 371810705408
Version:  2.0.50727.5420

Get System Information using WMI class in C#

The below C# function get system information using WMI class. You need to add reference

public static void SystemInformation()
{
    StringBuilder processorInfo = new StringBuilder(string.Empty);
    ManagementClass mgmntClass = new ManagementClass("Win32_OperatingSystem");
    ManagementObjectCollection mgmntObj = mgmntClass.GetInstances();
    PropertyDataCollection properties = mgmntClass.Properties;
    foreach (ManagementObject obj in mgmntObj)
    {
        foreach (PropertyData property in properties)
        {
            try
            {
                processorInfo.AppendLine(property.Name + ":  " +
                    obj.Properties[property.Name].Value.ToString());
            }
            catch
            {
            }
        }
        processorInfo.AppendLine();
    }
    Console.WriteLine(processorInfo);
    Console.ReadKey();
}

Output:

BootDevice:  DeviceHarddiskVolume1
BuildNumber:  7601
BuildType:  Multiprocessor Free
Caption:  Microsoft Windows Server 2008 R2 Enterprise 
CodeSet:  1252
CountryCode:  1
CreationClassName:  Win32_OperatingSystem
CSCreationClassName:  Win32_ComputerSystem
CSDVersion:  Service Pack 1
CSName:  DC1
CurrentTimeZone:  330
DataExecutionPrevention_32BitApplications:  True
DataExecutionPrevention_Available:  True
DataExecutionPrevention_Drivers:  True
DataExecutionPrevention_SupportPolicy:  3
Debug:  False
Description:  
Distributed:  False
EncryptionLevel:  256
ForegroundApplicationBoost:  2
FreePhysicalMemory:  284236
FreeSpaceInPagingFiles:  0
FreeVirtualMemory:  43384
InstallDate:  20140628143731.000000+330
LastBootUpTime:  20150826015035.016594+330
LocalDateTime:  20150827194243.001000+330
Locale:  0409
Manufacturer:  Microsoft Corporation
MaxNumberOfProcesses:  4294967295
MaxProcessMemorySize:  8589934464
MUILanguages:  System.String[]
Name:  Microsoft Windows Server 2008 R2 Enterprise |C:Windows|DeviceHarddisk0Partition3
NumberOfLicensedUsers:  0
NumberOfProcesses:  83
NumberOfUsers:  4
OperatingSystemSKU:  10
Organization:  
OSArchitecture:  64-bit
OSLanguage:  1033
OSProductSuite:  274
OSType:  18
Primary:  True
ProductType:  2
RegisteredUser:  Windows User
SerialNumber:  55041-507-1081103-84377
ServicePackMajorVersion:  1
ServicePackMinorVersion:  0
SizeStoredInPagingFiles:  0
Status:  OK
SuiteMask:  274
SystemDevice:  DeviceHarddiskVolume3
SystemDirectory:  C:Windowssystem32
SystemDrive:  C:
TotalVirtualMemorySize:  4093680
TotalVisibleMemorySize:  4095544
Version:  6.1.7601
WindowsDirectory:  C:Windows

You can also get Processor and Drive information by using the WMI classes Win32_Processor and Win32_LogicalDisk.


Advertisement

1 thought on “Get System Information in C#”

  1. Thanks for the code samples.
    Unfortunately all backslashes are lost, so the strings like “{1}ntt DriveType: {2}ntt DriveFormat: {3}ntt ” are ill-formatted.

    Reply

Leave a Comment