Home

Awesome

Hardware.Info

Battery, BIOS, CPU - processor, storage drive, keyboard, RAM - memory, monitor, motherboard, mouse, NIC - network adapter, printer, sound card - audio card, graphics card - video card. Hardware.Info is a .NET Standard 2.0 library and uses WMI on Windows, /dev, /proc, /sys on Linux and sysctl, system_profiler on macOS.

How to use:

  1. Include NuGet package from https://www.nuget.org/packages/Hardware.Info

     <ItemGroup>
         <PackageReference Include="Hardware.Info" Version="101.0.0.0" />
     </ItemGroup>
    
  2. Call RefreshAll() or one of the other Refresh*() methods:

     class Program
     {
         static IHardwareInfo hardwareInfo;
    
         static void Main(string[] _)
         {
             try
             {
                 hardwareInfo = new HardwareInfo();
    
                 //hardwareInfo.RefreshOperatingSystem();
                 //hardwareInfo.RefreshMemoryStatus();
                 //hardwareInfo.RefreshBatteryList();
                 //hardwareInfo.RefreshBIOSList();
                 //hardwareInfo.RefreshComputerSystemList();
                 //hardwareInfo.RefreshCPUList();
                 //hardwareInfo.RefreshDriveList();
                 //hardwareInfo.RefreshKeyboardList();
                 //hardwareInfo.RefreshMemoryList();
                 //hardwareInfo.RefreshMonitorList();
                 //hardwareInfo.RefreshMotherboardList();
                 //hardwareInfo.RefreshMouseList();
                 //hardwareInfo.RefreshNetworkAdapterList();
                 //hardwareInfo.RefreshPrinterList();
                 //hardwareInfo.RefreshSoundDeviceList();
                 //hardwareInfo.RefreshVideoControllerList();
    
                 hardwareInfo.RefreshAll();
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex);
             }
    
             Console.WriteLine(hardwareInfo.OperatingSystem);
    
             Console.WriteLine(hardwareInfo.MemoryStatus);
    
             foreach (var hardware in hardwareInfo.BatteryList)
                 Console.WriteLine(hardware);
    
             foreach (var hardware in hardwareInfo.BiosList)
                 Console.WriteLine(hardware);
    
             foreach (var hardware in hardwareInfo.ComputerSystemList)
                 Console.WriteLine(hardware);
    
             foreach (var cpu in hardwareInfo.CpuList)
             {
                 Console.WriteLine(cpu);
    
                 foreach (var cpuCore in cpu.CpuCoreList)
                     Console.WriteLine(cpuCore);
             }
    
             foreach (var drive in hardwareInfo.DriveList)
             {
                 Console.WriteLine(drive);
    
                 foreach (var partition in drive.PartitionList)
                 {
                     Console.WriteLine(partition);
    
                     foreach (var volume in partition.VolumeList)
                         Console.WriteLine(volume);
                 }
             }
    
             foreach (var hardware in hardwareInfo.KeyboardList)
                 Console.WriteLine(hardware);
    
             foreach (var hardware in hardwareInfo.MemoryList)
                 Console.WriteLine(hardware);
    
             foreach (var hardware in hardwareInfo.MonitorList)
                 Console.WriteLine(hardware);
    
             foreach (var hardware in hardwareInfo.MotherboardList)
                 Console.WriteLine(hardware);
    
             foreach (var hardware in hardwareInfo.MouseList)
                 Console.WriteLine(hardware);
    
             foreach (var hardware in hardwareInfo.NetworkAdapterList)
                 Console.WriteLine(hardware);
    
             foreach (var hardware in hardwareInfo.PrinterList)
                 Console.WriteLine(hardware);
    
             foreach (var hardware in hardwareInfo.SoundDeviceList)
                 Console.WriteLine(hardware);
    
             foreach (var hardware in hardwareInfo.VideoControllerList)
                 Console.WriteLine(hardware);
    
             foreach (var address in HardwareInfo.GetLocalIPv4Addresses(NetworkInterfaceType.Ethernet, OperationalStatus.Up))
                 Console.WriteLine(address);
    
             Console.WriteLine();
    
             foreach (var address in HardwareInfo.GetLocalIPv4Addresses(NetworkInterfaceType.Wireless80211))
                 Console.WriteLine(address);
    
             Console.WriteLine();
    
             foreach (var address in HardwareInfo.GetLocalIPv4Addresses(OperationalStatus.Up))
                 Console.WriteLine(address);
    
             Console.WriteLine();
    
             foreach (var address in HardwareInfo.GetLocalIPv4Addresses())
                 Console.WriteLine(address);
    
             Console.ReadLine();
         }
     }
    

Known issues

21 second delay on first use in Windows

Hardware.Info uses WMI (Windows Management Instrumentation) on Windows OS. For certain queries WMI takes 21 seconds to initialize the first time you use it, after that all subsequent queries will execute immediately. If WMI isn't used for 15 minutes it will have to be initialized again the next time you use it.

The 21 second initialization delay is caused by RPC that WMI uses internally. In RPC documentation it says that the RPC/TCP time-out interval is defined with a SCMApiConnectionParam registry value located at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control and that the default value is set to 21,000 (21 seconds).

You can avoid the 21 second delay by excluding the queries that cause it (see Settings).

Invalid NetworkAdapter.Speed in Windows

Sometimes NetworkAdapter.Speed in Win32_NetworkAdapter can be 0 or long.MaxValue. The correct value can be retrived from CurrentBandwidth in Win32_PerfFormattedData_Tcpip_NetworkAdapter but unfortunately reading from Win32_PerfFormattedData_Tcpip_NetworkAdapter causes a 21 second delay on the first read, like mentioned in the previous paragraph. Calling RefreshNetworkAdapterList with includeBytesPersec = true will also read the CurrentBandwidth.

WmiNetUtilsHelper will throw an exception in Windows if publish settings use <PublishTrimmed>true</PublishTrimmed>

This is a known error: https://github.com/dotnet/core/issues/7051#issuecomment-1071484354

Settings

Constructor settings:

HardwareInfo(bool useAsteriskInWMI = true, TimeSpan? timeoutInWMI = null)

The construcotr accepts two settings for WMI:

Refresh methods settings:

RefreshCPUList(
    bool includePercentProcessorTime = true, 
    int millisecondsDelayBetweenTwoMeasurements = 500)

RefreshNetworkAdapterList(
    bool includeBytesPersec = true, 
    bool includeNetworkAdapterConfiguration = true, 
    int millisecondsDelayBetweenTwoMeasurements = 1000)

Setting includePercentProcessorTime and includeBytesPersec to false will exclude the queries that:

Setting includeNetworkAdapterConfiguration to false has only a small impact on performance.

Delay in milliseconds between two measurements in Linux:

For PercentProcessorTime in Linux:

string[] cpuUsageLineLast = TryReadLinesFromFile("/proc/stat");
Task.Delay(millisecondsDelayBetweenTwoMeasurements).Wait();
string[] cpuUsageLineNow = TryReadLinesFromFile("/proc/stat");

If includePercentProcessorTime is false, millisecondsDelayBetweenTwoMeasurements has no effect.

For BytesSentPersec and BytesReceivedPersec in Linux:

string[] procNetDevLast = TryReadLinesFromFile("/proc/net/dev");
Task.Delay(millisecondsDelayBetweenTwoMeasurements).Wait();
string[] procNetDevNow = TryReadLinesFromFile("/proc/net/dev");

If includeBytesPersec is false, millisecondsDelayBetweenTwoMeasurements has no effect.

Benchmarks

Windows 8.1 (Intel i5-2500, 8 GB RAM):

MethodMeanErrorStdDev
RefreshMemoryStatus947.8 ns3.77 ns3.53 ns
RefreshBatteryList1,811,885.7 ns12,921.05 ns11,454.17 ns
RefreshBIOSList2,086,001.0 ns23,896.69 ns22,352.98 ns
RefreshCPUList1,543,579,005.2 ns2,405,376.47 ns2,132,303.59 ns
RefreshDriveList409,137,516.3 ns8,612,410.99 ns25,258,710.57 ns
RefreshKeyboardList5,568,039.5 ns44,228.57 ns41,371.43 ns
RefreshMemoryList2,120,024.5 ns26,103.39 ns24,417.13 ns
RefreshMonitorList5,669,237.8 ns50,801.76 ns45,034.44 ns
RefreshMotherboardList1,965,222.9 ns14,387.30 ns13,457.89 ns
RefreshMouseList6,003,924.9 ns60,725.05 ns50,708.17 ns
RefreshNetworkAdapterList1,412,244,738.6 ns14,681,615.28 ns12,259,813.69 ns
RefreshPrinterList28,244,822.2 ns143,359.60 ns134,098.66 ns
RefreshSoundDeviceList3,608,577.5 ns68,688.62 ns73,496.06 ns
RefreshVideoControllerList11,568,549.2 ns54,666.07 ns48,460.05 ns

Windows 10 (AMD Ryzen 5 5600G, 32 GB RAM):

MethodMeanErrorStdDev
RefreshOperatingSystem2.946 ns0.0052 ns0.0047 ns
RefreshMemoryStatus460.552 ns4.4810 ns3.9723 ns
RefreshBatteryList1,624,392.057 ns22,526.9314 ns21,071.7057 ns
RefreshBIOSList1,785,673.828 ns8,812.8115 ns8,243.5094 ns
RefreshCPUList1,964,995,539.000 ns171,465,934.5051 ns505,571,176.5574 ns
RefreshDriveList62,452,668.148 ns342,662.0413 ns320,526.2860 ns
RefreshKeyboardList4,303,528.516 ns47,355.1733 ns41,979.1277 ns
RefreshMemoryList1,926,931.367 ns19,754.4179 ns18,478.2948 ns
RefreshMonitorList3,884,362.370 ns29,422.1438 ns27,521.4916 ns
RefreshMotherboardList1,782,235.664 ns12,974.2296 ns12,136.1024 ns
RefreshMouseList4,700,086.615 ns44,435.0631 ns41,564.5856 ns
RefreshNetworkAdapterList945,004,493.333 ns8,568,978.4607 ns8,015,427.7687 ns
RefreshPrinterList48,126,103.030 ns729,958.0933 ns682,803.2534 ns
RefreshSoundDeviceList4,154,082.924 ns46,922.5501 ns41,595.6184 ns
RefreshVideoControllerList8,784,372.500 ns125,080.5212 ns117,000.3971 ns

Version history: