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.InfoREADME
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:
-
Include
Hardware.InfoorHardware.Info.AotNuGet packagea. 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> -
Call
RefreshAll()or one of the otherRefresh*()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:
-
timeoutInWMIsets theTimeoutproperty of theEnumerationOptionsin theManagementObjectSearcherthat executes each query. The default value isEnumerationOptions.InfiniteTimeout. There are one or more queries for each hardware component, so there are more than 16 queries executed onRefreshAll(). If a query reaches the timeout it will throw aSystem.Management.ManagementExceptionexception whereErrorCodewill beSystem.Management.ManagementStatus.Timedout. If you set thetimeoutInWMIthen use atry-catchblock 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
docs-writer
98.6k`docs-writer` skill instructions As an expert technical writer and editor for the Gemini CLI project, you produce accurate, clear, and consistent documentation. When asked to write, edit, or revie
model-usage
328.7kUse CodexBar CLI local cost usage to summarize per-model usage for Codex or Claude, including the current (most recent) model or a full model breakdown. Trigger when asked for model-level usage/cost data from codexbar, or when you need a scriptable per-model summary from codexbar cost JSON.
Design
Campus Second-Hand Trading Platform \- General Design Document (v5.0 \- React Architecture \- Complete Final Version)1\. System Overall Design 1.1. Project Overview This project aims t
arscontexta
2.8kClaude Code plugin that generates individualized knowledge systems from conversation. You describe how you think and work, have a conversation and get a complete second brain as markdown files you own.
