Skip to content

Commit

Permalink
Version 1.1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinjinov committed Aug 25, 2021
1 parent 35858d6 commit 4b1331d
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 37 deletions.
6 changes: 3 additions & 3 deletions Hardware.Info/Components/Battery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand All @@ -55,7 +55,7 @@ public string BatteryStatusDescription
_ => string.Empty
};

internal set => batteryStatusDescription = value;
set => _batteryStatusDescription = value;
}

public override string ToString()
Expand Down
2 changes: 1 addition & 1 deletion Hardware.Info/Hardware.Info.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PropertyGroup>
<PackageId>Hardware.Info</PackageId>
<Product>Hardware.Info</Product>
<Version>1.1.0.1</Version>
<Version>1.1.1.0</Version>
<Authors>Jinjinov</Authors>
<Description>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.</Description>
<PackageTags>Computer;Device;Hardware;Info;Information;NET Standard;Windows;Linux;macOS</PackageTags>
Expand Down
14 changes: 7 additions & 7 deletions Hardware.Info/HardwareInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,17 @@ public void RefreshAll()

#region Static

private static bool pingInProgress;
private static Action<bool>? OnPingComplete;
private static bool _pingInProgress;
private static Action<bool>? _onPingComplete;

public static void Ping(string hostNameOrAddress, Action<bool> onPingComplete)
{
if (pingInProgress)
if (_pingInProgress)
return;

pingInProgress = true;
_pingInProgress = true;

OnPingComplete = onPingComplete;
_onPingComplete = onPingComplete;

using Ping pingSender = new Ping();
pingSender.PingCompleted += new PingCompletedEventHandler(PingCompleted);
Expand All @@ -109,7 +109,7 @@ public static void Ping(string hostNameOrAddress, Action<bool> onPingComplete)

private static void PingCompleted(object sender, PingCompletedEventArgs e)
{
pingInProgress = false;
_pingInProgress = false;

bool success = true;

Expand All @@ -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<IPAddress> GetLocalIPv4Addresses()
Expand Down
4 changes: 4 additions & 0 deletions Hardware.Info/IHardwareInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Hardware.Info
public interface IHardwareInfo
{
MemoryStatus MemoryStatus { get; }

List<Battery> BatteryList { get; }
List<BIOS> BiosList { get; }
List<CPU> CpuList { get; }
Expand All @@ -18,8 +19,11 @@ public interface IHardwareInfo
List<Printer> PrinterList { get; }
List<SoundDevice> SoundDeviceList { get; }
List<VideoController> VideoControllerList { get; }

void RefreshAll();

void RefreshMemoryStatus();

void RefreshBatteryList();
void RefreshBIOSList();
void RefreshCPUList(bool includePercentProcessorTime = true);
Expand Down
12 changes: 6 additions & 6 deletions Hardware.Info/Linux/HardwareInfoRetrieval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions Hardware.Info/Mac/HardwareInfoRetrieval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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<Battery> GetBatteryList()
Expand Down
30 changes: 15 additions & 15 deletions Hardware.Info/Windows/HardwareInfoRetrieval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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<T>(object obj) where T : struct
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<PackageReference Include="Hardware.Info" Version="1.1.0.1" />
<PackageReference Include="Hardware.Info" Version="1.1.1.0" />

2. Call `RefreshAll()` or one of the other `Refresh*()` methods:

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4b1331d

Please sign in to comment.