Hardware.Info 是一个基于 .NET Standard 2.0 的跨平台库,提供了硬件信息查询的功能,支持 Windows、Linux 和 macOS 操作系统。该库通过不同平台下的系统接口获取硬件信息,包括电池、电池管理、BIOS、CPU、存储驱动、键盘、内存、显示器、主板、鼠标、网络适配器、打印机、声音卡、显卡等硬件组件的详细数据。
Hardware.Info
/dev
/proc
/sys
sysctl
system_profiler
第一步,老规矩,先安装
Install-Package Hardware.Info
using Hardware.Info;using System.Net.NetworkInformation;static IHardwareInfo hardwareInfo;static void Main(string[] args){ try { hardwareInfo = new HardwareInfo(); //hardwareInfo.RefreshOperatingSystem(); //..... //刷新所有硬件信息 hardwareInfo.RefreshAll(); } catch (Exception ex) { Console.WriteLine(ex); } Console.WriteLine("输出操作系统信息" + Environment.NewLine); Console.WriteLine(hardwareInfo.OperatingSystem); Console.WriteLine("输出内存状态信息" + Environment.NewLine); Console.WriteLine(hardwareInfo.MemoryStatus); Console.WriteLine("输出电池信息" + Environment.NewLine); foreach (var hardware in hardwareInfo.BatteryList) Console.WriteLine(hardware); Console.WriteLine("输出 BIOS 信息" + Environment.NewLine); foreach (var hardware in hardwareInfo.BiosList) Console.WriteLine(hardware); Console.WriteLine("输出计算机系统信息" + Environment.NewLine); foreach (var hardware in hardwareInfo.ComputerSystemList) Console.WriteLine(hardware); Console.WriteLine("输出 CPU 信息" + Environment.NewLine); foreach (var cpu in hardwareInfo.CpuList) { Console.WriteLine(cpu); Console.WriteLine("输出 CPU 核心信息" + Environment.NewLine); foreach (var cpuCore in cpu.CpuCoreList) Console.WriteLine(cpuCore); } Console.WriteLine("输出磁盘驱动器信息" + Environment.NewLine); foreach (var drive in hardwareInfo.DriveList) { Console.WriteLine(drive); Console.WriteLine("输出驱动器分区信息" + Environment.NewLine); foreach (var partition in drive.PartitionList) { Console.WriteLine(partition); Console.WriteLine("输出分区卷信息" + Environment.NewLine); foreach (var volume in partition.VolumeList) Console.WriteLine(volume); } } Console.WriteLine("输出键盘信息" + Environment.NewLine); foreach (var hardware in hardwareInfo.KeyboardList) Console.WriteLine(hardware); Console.WriteLine("输出内存信息" + Environment.NewLine); foreach (var hardware in hardwareInfo.MemoryList) Console.WriteLine(hardware); Console.WriteLine("输出显示器信息" + Environment.NewLine); foreach (var hardware in hardwareInfo.MonitorList) Console.WriteLine(hardware); Console.WriteLine("输出主板信息" + Environment.NewLine); foreach (var hardware in hardwareInfo.MotherboardList) Console.WriteLine(hardware); Console.WriteLine("输出鼠标信息" + Environment.NewLine); foreach (var hardware in hardwareInfo.MouseList) Console.WriteLine(hardware); Console.WriteLine("输出网络适配器信息" + Environment.NewLine); foreach (var hardware in hardwareInfo.NetworkAdapterList) Console.WriteLine(hardware); Console.WriteLine("输出打印机信息" + Environment.NewLine); foreach (var hardware in hardwareInfo.PrinterList) Console.WriteLine(hardware); Console.WriteLine("输出声音设备信息" + Environment.NewLine); foreach (var hardware in hardwareInfo.SoundDeviceList) Console.WriteLine(hardware); Console.WriteLine("输出显卡信息" + Environment.NewLine); foreach (var hardware in hardwareInfo.VideoControllerList) Console.WriteLine(hardware); Console.WriteLine("输出 Ethernet 类型并且状态为 Up 的本地 IPv4 地址" + Environment.NewLine); foreach (var address in HardwareInfo.GetLocalIPv4Addresses(NetworkInterfaceType.Ethernet, OperationalStatus.Up)) Console.WriteLine(address); Console.WriteLine(); Console.WriteLine("输出 Wireless80211 类型的本地 IPv4 地址" + Environment.NewLine); foreach (var address in HardwareInfo.GetLocalIPv4Addresses(NetworkInterfaceType.Wireless80211)) Console.WriteLine(address); Console.WriteLine(); Console.WriteLine("输出状态为 Up 的网络接口卡的本地 IPv4 地址" + Environment.NewLine); foreach (var address in HardwareInfo.GetLocalIPv4Addresses(OperationalStatus.Up)) Console.WriteLine(address); Console.WriteLine(); Console.WriteLine("输出所有网络接口卡的本地 IPv4 地址" + Environment.NewLine); foreach (var address in HardwareInfo.GetLocalIPv4Addresses()) Console.WriteLine(address); Console.WriteLine(); Console.WriteLine("输出计算机当前时间" + Environment.NewLine); Console.WriteLine(DateTime.Now); Console.WriteLine("输出系统启动时间" + Environment.NewLine); Console.WriteLine(Environment.TickCount);}
using Hardware.Info;
using System.Net.NetworkInformation;
static IHardwareInfo hardwareInfo;
static void Main(string[] args)
{
try
hardwareInfo = new HardwareInfo();
//hardwareInfo.RefreshOperatingSystem();
//.....
//刷新所有硬件信息
hardwareInfo.RefreshAll();
}
catch (Exception ex)
Console.WriteLine(ex);
Console.WriteLine("输出操作系统信息" + Environment.NewLine);
Console.WriteLine(hardwareInfo.OperatingSystem);
Console.WriteLine("输出内存状态信息" + Environment.NewLine);
Console.WriteLine(hardwareInfo.MemoryStatus);
Console.WriteLine("输出电池信息" + Environment.NewLine);
foreach (var hardware in hardwareInfo.BatteryList)
Console.WriteLine(hardware);
Console.WriteLine("输出 BIOS 信息" + Environment.NewLine);
foreach (var hardware in hardwareInfo.BiosList)
Console.WriteLine("输出计算机系统信息" + Environment.NewLine);
foreach (var hardware in hardwareInfo.ComputerSystemList)
Console.WriteLine("输出 CPU 信息" + Environment.NewLine);
foreach (var cpu in hardwareInfo.CpuList)
Console.WriteLine(cpu);
Console.WriteLine("输出 CPU 核心信息" + Environment.NewLine);
foreach (var cpuCore in cpu.CpuCoreList)
Console.WriteLine(cpuCore);
Console.WriteLine("输出磁盘驱动器信息" + Environment.NewLine);
foreach (var drive in hardwareInfo.DriveList)
Console.WriteLine(drive);
Console.WriteLine("输出驱动器分区信息" + Environment.NewLine);
foreach (var partition in drive.PartitionList)
Console.WriteLine(partition);
Console.WriteLine("输出分区卷信息" + Environment.NewLine);
foreach (var volume in partition.VolumeList)
Console.WriteLine(volume);
Console.WriteLine("输出键盘信息" + Environment.NewLine);
foreach (var hardware in hardwareInfo.KeyboardList)
Console.WriteLine("输出内存信息" + Environment.NewLine);
foreach (var hardware in hardwareInfo.MemoryList)
Console.WriteLine("输出显示器信息" + Environment.NewLine);
foreach (var hardware in hardwareInfo.MonitorList)
Console.WriteLine("输出主板信息" + Environment.NewLine);
foreach (var hardware in hardwareInfo.MotherboardList)
Console.WriteLine("输出鼠标信息" + Environment.NewLine);
foreach (var hardware in hardwareInfo.MouseList)
Console.WriteLine("输出网络适配器信息" + Environment.NewLine);
foreach (var hardware in hardwareInfo.NetworkAdapterList)
Console.WriteLine("输出打印机信息" + Environment.NewLine);
foreach (var hardware in hardwareInfo.PrinterList)
Console.WriteLine("输出声音设备信息" + Environment.NewLine);
foreach (var hardware in hardwareInfo.SoundDeviceList)
Console.WriteLine("输出显卡信息" + Environment.NewLine);
foreach (var hardware in hardwareInfo.VideoControllerList)
Console.WriteLine("输出 Ethernet 类型并且状态为 Up 的本地 IPv4 地址" + Environment.NewLine);
foreach (var address in HardwareInfo.GetLocalIPv4Addresses(NetworkInterfaceType.Ethernet, OperationalStatus.Up))
Console.WriteLine(address);
Console.WriteLine();
Console.WriteLine("输出 Wireless80211 类型的本地 IPv4 地址" + Environment.NewLine);
foreach (var address in HardwareInfo.GetLocalIPv4Addresses(NetworkInterfaceType.Wireless80211))
Console.WriteLine("输出状态为 Up 的网络接口卡的本地 IPv4 地址" + Environment.NewLine);
foreach (var address in HardwareInfo.GetLocalIPv4Addresses(OperationalStatus.Up))
Console.WriteLine("输出所有网络接口卡的本地 IPv4 地址" + Environment.NewLine);
foreach (var address in HardwareInfo.GetLocalIPv4Addresses())
Console.WriteLine("输出计算机当前时间" + Environment.NewLine);
Console.WriteLine(DateTime.Now);
Console.WriteLine("输出系统启动时间" + Environment.NewLine);
Console.WriteLine(Environment.TickCount);
以项目为准,仅供参考,项目地址:https://github.com/hardware-info/hardware-info
Hardware.Info 是一个强大的跨平台硬件信息查询库,支持 Windows、Linux 和 macOS 操作系统,能方便地获取多种硬件信息。尽管存在一些已知问题,比如首次查询的延迟和部分数据无效的情况,但通过合适的配置和方法,可以有效解决这些问题,提供了强大的硬件信息获取能力。
原文链接:https://www.cnblogs.com/netcore5/p/18711795
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728