SkillAgentSearch skills...

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.

Install / Use

/learn @Jinjinov/Hardware.Info
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

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.

Hardware.Info.Aot uses WmiLight instead of System.Management and supports AOT.

How to use:

  1. Include Hardware.Info or Hardware.Info.Aot NuGet package

    a. https://www.nuget.org/packages/Hardware.Info

         <ItemGroup>
             <PackageReference Include="Hardware.Info" Version="101.1.1.1" />
         </ItemGroup>
    

    b. https://www.nuget.org/packages/Hardware.Info.Aot

         <ItemGroup>
             <PackageReference Include="Hardware.Info.Aot" Version="101.1.1.1" />
         </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

PerformanceCounter may crash msvcr80.dll with an invalid-parameter exception

When using PerformanceCounter on a machine without the Visual C++ 2005 CRT installed, the native shim pulls in msvcr80.dll and may crash with an invalid-parameter exception (0xc000000d).

To skip the usage of PerformanceCounter set includePerformanceCounter to false (see Settings).

Settings

Constructor settings:

HardwareInfo(TimeSpan? timeoutInWMI = null)

The construcotr accepts a setting for WMI:

  • timeoutInWMI sets the Timeout property of the EnumerationOptions in the ManagementObjectSearcher that executes each query. The default value is EnumerationOptions.InfiniteTimeout. There are one or more queries for each hardware component, so there are more than 16 queries executed on RefreshAll(). If a query reaches the timeout it will throw a System.Management.ManagementException exception where ErrorCode will be System.Management.ManagementStatus.Timedout. If you set the timeoutInWMI then use a try-catch block like this:

      IHardwareInfo hardwareInfo;
    
      try
      {
          hardwareInfo = new HardwareInfo(timeoutInWMI: TimeSpan.FromMilliseconds(100));
    
          hardwareInfo.RefreshAll();
      }
      catch (ManagementException ex) when (ex.ErrorCode == ManagementStatus.Timedout)
      {
          Console.WriteLine(ex);
      }
    

Refresh methods settings:

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

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

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

  • cause a 21 second delay the first time they are called in Windows
  • cause a 1 second delay every time they are called in Linux

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.

Setting includePerformanceCounter to false excludes PerformanceCounter in Windows and avoids the exception in case Visual C++ 2005 CRT is not installed.

Benchma

Related Skills

View on GitHub
GitHub Stars683
CategoryContent
Updated3d ago
Forks106

Languages

C#

Security Score

100/100

Audited on Mar 18, 2026

No findings