From 4b1331d2bf8cfaa35cf39138244e6d6026a0e666 Mon Sep 17 00:00:00 2001 From: Urban Date: Wed, 25 Aug 2021 19:18:46 +0200 Subject: [PATCH] Version 1.1.1.0 --- Hardware.Info/Components/Battery.cs | 6 ++-- Hardware.Info/Hardware.Info.csproj | 2 +- Hardware.Info/HardwareInfo.cs | 14 ++++----- Hardware.Info/IHardwareInfo.cs | 4 +++ Hardware.Info/Linux/HardwareInfoRetrieval.cs | 12 ++++---- Hardware.Info/Mac/HardwareInfoRetrieval.cs | 8 ++--- .../Windows/HardwareInfoRetrieval.cs | 30 +++++++++---------- README.md | 4 ++- 8 files changed, 43 insertions(+), 37 deletions(-) diff --git a/Hardware.Info/Components/Battery.cs b/Hardware.Info/Components/Battery.cs index 984cff6..2d773f9 100644 --- a/Hardware.Info/Components/Battery.cs +++ b/Hardware.Info/Components/Battery.cs @@ -35,11 +35,11 @@ public class Battery // Remaining time to charge the battery fully in minutes at the current charging rate and usage. public UInt32 TimeToFullCharge { get; set; } - private string batteryStatusDescription = string.Empty; + private string _batteryStatusDescription = string.Empty; public string BatteryStatusDescription { - get => !string.IsNullOrEmpty(batteryStatusDescription) ? batteryStatusDescription : BatteryStatus switch + get => !string.IsNullOrEmpty(_batteryStatusDescription) ? _batteryStatusDescription : BatteryStatus switch { 1 => "The battery is discharging", 2 => "The system has access to AC so no battery is being discharged. However, the battery is not necessarily charging.", @@ -55,7 +55,7 @@ public string BatteryStatusDescription _ => string.Empty }; - internal set => batteryStatusDescription = value; + set => _batteryStatusDescription = value; } public override string ToString() diff --git a/Hardware.Info/Hardware.Info.csproj b/Hardware.Info/Hardware.Info.csproj index 5f43f09..711a74f 100644 --- a/Hardware.Info/Hardware.Info.csproj +++ b/Hardware.Info/Hardware.Info.csproj @@ -9,7 +9,7 @@ Hardware.Info Hardware.Info - 1.1.0.1 + 1.1.1.0 Jinjinov 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. Computer;Device;Hardware;Info;Information;NET Standard;Windows;Linux;macOS diff --git a/Hardware.Info/HardwareInfo.cs b/Hardware.Info/HardwareInfo.cs index 03b838d..0541d98 100644 --- a/Hardware.Info/HardwareInfo.cs +++ b/Hardware.Info/HardwareInfo.cs @@ -83,17 +83,17 @@ public void RefreshAll() #region Static - private static bool pingInProgress; - private static Action? OnPingComplete; + private static bool _pingInProgress; + private static Action? _onPingComplete; public static void Ping(string hostNameOrAddress, Action onPingComplete) { - if (pingInProgress) + if (_pingInProgress) return; - pingInProgress = true; + _pingInProgress = true; - OnPingComplete = onPingComplete; + _onPingComplete = onPingComplete; using Ping pingSender = new Ping(); pingSender.PingCompleted += new PingCompletedEventHandler(PingCompleted); @@ -109,7 +109,7 @@ public static void Ping(string hostNameOrAddress, Action onPingComplete) private static void PingCompleted(object sender, PingCompletedEventArgs e) { - pingInProgress = false; + _pingInProgress = false; bool success = true; @@ -126,7 +126,7 @@ private static void PingCompleted(object sender, PingCompletedEventArgs e) else if (reply.Status != IPStatus.Success) success = false; - OnPingComplete?.Invoke(success); + _onPingComplete?.Invoke(success); } public static IEnumerable GetLocalIPv4Addresses() diff --git a/Hardware.Info/IHardwareInfo.cs b/Hardware.Info/IHardwareInfo.cs index 6827b2f..4b91b54 100644 --- a/Hardware.Info/IHardwareInfo.cs +++ b/Hardware.Info/IHardwareInfo.cs @@ -5,6 +5,7 @@ namespace Hardware.Info public interface IHardwareInfo { MemoryStatus MemoryStatus { get; } + List BatteryList { get; } List BiosList { get; } List CpuList { get; } @@ -18,8 +19,11 @@ public interface IHardwareInfo List PrinterList { get; } List SoundDeviceList { get; } List VideoControllerList { get; } + void RefreshAll(); + void RefreshMemoryStatus(); + void RefreshBatteryList(); void RefreshBIOSList(); void RefreshCPUList(bool includePercentProcessorTime = true); diff --git a/Hardware.Info/Linux/HardwareInfoRetrieval.cs b/Hardware.Info/Linux/HardwareInfoRetrieval.cs index f89f5b5..67b21aa 100644 --- a/Hardware.Info/Linux/HardwareInfoRetrieval.cs +++ b/Hardware.Info/Linux/HardwareInfoRetrieval.cs @@ -10,18 +10,18 @@ namespace Hardware.Info.Linux { internal class HardwareInfoRetrieval : HardwareInfoBase, IHardwareInfoRetrieval { - private readonly MemoryStatus memoryStatus = new MemoryStatus(); + private readonly MemoryStatus _memoryStatus = new MemoryStatus(); public MemoryStatus GetMemoryStatus() { string[] meminfo = TryReadFileLines("/proc/meminfo"); - memoryStatus.TotalPhysical = GetBytesFromLine(meminfo, "MemTotal:"); - memoryStatus.AvailablePhysical = GetBytesFromLine(meminfo, "MemAvailable:"); - memoryStatus.TotalVirtual = GetBytesFromLine(meminfo, "SwapTotal:"); - memoryStatus.AvailableVirtual = GetBytesFromLine(meminfo, "SwapFree:"); + _memoryStatus.TotalPhysical = GetBytesFromLine(meminfo, "MemTotal:"); + _memoryStatus.AvailablePhysical = GetBytesFromLine(meminfo, "MemAvailable:"); + _memoryStatus.TotalVirtual = GetBytesFromLine(meminfo, "SwapTotal:"); + _memoryStatus.AvailableVirtual = GetBytesFromLine(meminfo, "SwapFree:"); - return memoryStatus; + return _memoryStatus; } private ulong GetBytesFromLine(string[] meminfo, string token) diff --git a/Hardware.Info/Mac/HardwareInfoRetrieval.cs b/Hardware.Info/Mac/HardwareInfoRetrieval.cs index 9d8e4fe..677ad5b 100644 --- a/Hardware.Info/Mac/HardwareInfoRetrieval.cs +++ b/Hardware.Info/Mac/HardwareInfoRetrieval.cs @@ -18,11 +18,11 @@ namespace Hardware.Info.Mac { internal class HardwareInfoRetrieval : HardwareInfoBase, IHardwareInfoRetrieval { + private readonly MemoryStatus _memoryStatus = new MemoryStatus(); + [DllImport("libc")] static extern int sysctlbyname(string name, out IntPtr oldp, ref IntPtr oldlenp, IntPtr newp, IntPtr newlen); - private readonly MemoryStatus memoryStatus = new MemoryStatus(); - /* public HardwareInfoRetrieval() { @@ -176,10 +176,10 @@ public MemoryStatus GetMemoryStatus() if (sysctlbyname("hw.memsize", out IntPtr lineSize, ref SizeOfLineSize, IntPtr.Zero, IntPtr.Zero) == 0) { - memoryStatus.TotalPhysical = (ulong)lineSize.ToInt64(); + _memoryStatus.TotalPhysical = (ulong)lineSize.ToInt64(); } - return memoryStatus; + return _memoryStatus; } public List GetBatteryList() diff --git a/Hardware.Info/Windows/HardwareInfoRetrieval.cs b/Hardware.Info/Windows/HardwareInfoRetrieval.cs index 2107fbb..34a2e4e 100644 --- a/Hardware.Info/Windows/HardwareInfoRetrieval.cs +++ b/Hardware.Info/Windows/HardwareInfoRetrieval.cs @@ -27,10 +27,14 @@ public MEMORYSTATUSEX() internal class HardwareInfoRetrieval : HardwareInfoBase, IHardwareInfoRetrieval { + private readonly MEMORYSTATUSEX _memoryStatusEx = new MEMORYSTATUSEX(); + + private readonly MemoryStatus _memoryStatus = new MemoryStatus(); + public bool UseAsteriskInWMI { get; set; } - readonly string _managementScope = "root\\cimv2"; - readonly EnumerationOptions _enumerationOptions = new EnumerationOptions() { ReturnImmediately = true, Rewindable = false, Timeout = EnumerationOptions.InfiniteTimeout }; + private readonly string _managementScope = "root\\cimv2"; + private readonly EnumerationOptions _enumerationOptions = new EnumerationOptions() { ReturnImmediately = true, Rewindable = false, Timeout = EnumerationOptions.InfiniteTimeout }; public HardwareInfoRetrieval(TimeSpan? enumerationOptionsTimeout = null) { @@ -40,28 +44,24 @@ public HardwareInfoRetrieval(TimeSpan? enumerationOptionsTimeout = null) _enumerationOptions = new EnumerationOptions() { ReturnImmediately = true, Rewindable = false, Timeout = enumerationOptionsTimeout.Value }; } - readonly MEMORYSTATUSEX memoryStatusEx = new MEMORYSTATUSEX(); - [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer); - readonly MemoryStatus memoryStatus = new MemoryStatus(); - public MemoryStatus GetMemoryStatus() { - if (GlobalMemoryStatusEx(memoryStatusEx)) + if (GlobalMemoryStatusEx(_memoryStatusEx)) { - memoryStatus.TotalPhysical = memoryStatusEx.ullTotalPhys; - memoryStatus.AvailablePhysical = memoryStatusEx.ullAvailPhys; - memoryStatus.TotalPageFile = memoryStatusEx.ullTotalPageFile; - memoryStatus.AvailablePageFile = memoryStatusEx.ullAvailPageFile; - memoryStatus.TotalVirtual = memoryStatusEx.ullTotalVirtual; - memoryStatus.AvailableVirtual = memoryStatusEx.ullAvailVirtual; - memoryStatus.AvailableExtendedVirtual = memoryStatusEx.ullAvailExtendedVirtual; + _memoryStatus.TotalPhysical = _memoryStatusEx.ullTotalPhys; + _memoryStatus.AvailablePhysical = _memoryStatusEx.ullAvailPhys; + _memoryStatus.TotalPageFile = _memoryStatusEx.ullTotalPageFile; + _memoryStatus.AvailablePageFile = _memoryStatusEx.ullAvailPageFile; + _memoryStatus.TotalVirtual = _memoryStatusEx.ullTotalVirtual; + _memoryStatus.AvailableVirtual = _memoryStatusEx.ullAvailVirtual; + _memoryStatus.AvailableExtendedVirtual = _memoryStatusEx.ullAvailExtendedVirtual; } - return memoryStatus; + return _memoryStatus; } public static T GetPropertyValue(object obj) where T : struct diff --git a/README.md b/README.md index 907a39f..2164214 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Battery, BIOS, CPU - processor, storage drive, keyboard, RAM - memory, monitor, 1. Include NuGet package from https://www.nuget.org/packages/Hardware.Info - + 2. Call `RefreshAll()` or one of the other `Refresh*()` methods: @@ -141,6 +141,8 @@ In these two methods you can exclude some slow queries by setting the parameters ## Version history: +- 1.1.1.0: + - Added `IHardwareInfo` so that `HardwareInfo` can be mocked - by [@240026763]( https://github.com/240026763 ) - 1.1.0.1: - Added two settings for WMI queries in Windows - Added three settings to exclude slow queries in Windows, macOS, Linux