Skip to content

Commit

Permalink
Version 10.0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Urban committed Apr 29, 2022
1 parent 2b6c65f commit 05213aa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
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>10.0.0.0</Version>
<Version>10.0.0.1</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
20 changes: 10 additions & 10 deletions Hardware.Info/HardwareInfoBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal static string ReadProcessOutput(string cmd, string args)
}
}

internal static string TryReadFileText(string path)
internal static string TryReadTextFromFile(string path)
{
try
{
Expand All @@ -52,22 +52,22 @@ internal static string TryReadFileText(string path)
}
}

internal static bool TryReadIntegerFromFileText(out uint value, params string[] possiblePaths)
internal static uint TryReadIntegerFromFile(params string[] possiblePaths)
{
foreach(var path in possiblePaths)
foreach (string path in possiblePaths)
{
string data = TryReadFileText(path);
if(uint.TryParse(data, out uint integer))
string text = TryReadTextFromFile(path);

if (uint.TryParse(text, out uint integer))
{
value = integer;
return true;
return integer;
}
}
value = 0;
return false;

return 0;
}

internal static string[] TryReadFileLines(string path)
internal static string[] TryReadLinesFromFile(string path)
{
try
{
Expand Down
34 changes: 17 additions & 17 deletions Hardware.Info/Linux/HardwareInfoRetrieval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class HardwareInfoRetrieval : HardwareInfoBase, IHardwareInfoRetrieval

public MemoryStatus GetMemoryStatus()
{
string[] meminfo = TryReadFileLines("/proc/meminfo");
string[] meminfo = TryReadLinesFromFile("/proc/meminfo");

_memoryStatus.TotalPhysical = GetBytesFromLine(meminfo, "MemTotal:");
_memoryStatus.AvailablePhysical = GetBytesFromLine(meminfo, "MemAvailable:");
Expand Down Expand Up @@ -68,10 +68,10 @@ public List<Battery> GetBatteryList()
// /sys/class/power_supply/BAT0/manufacturer = Sony Corp.
// /sys/class/power_supply/BAT0/serial_number =

TryReadIntegerFromFileText(out uint powerNow, "/sys/class/power_supply/BAT0/power_now", "/sys/class/power_supply/BAT0/voltage_now");
TryReadIntegerFromFileText(out uint designCapacity, "/sys/class/power_supply/BAT0/energy_full_design", "/sys/class/power_supply/BAT0/charge_full_design");
TryReadIntegerFromFileText(out uint fullChargeCapacity, "/sys/class/power_supply/BAT0/energy_full", "/sys/class/power_supply/BAT0/charge_full");
TryReadIntegerFromFileText(out uint energyNow, "/sys/class/power_supply/BAT0/energy_now", "/sys/class/power_supply/BAT0/charge_now");
uint powerNow = TryReadIntegerFromFile("/sys/class/power_supply/BAT0/power_now", "/sys/class/power_supply/BAT0/voltage_now");
uint designCapacity = TryReadIntegerFromFile("/sys/class/power_supply/BAT0/energy_full_design", "/sys/class/power_supply/BAT0/charge_full_design");
uint fullChargeCapacity = TryReadIntegerFromFile("/sys/class/power_supply/BAT0/energy_full", "/sys/class/power_supply/BAT0/charge_full");
uint energyNow = TryReadIntegerFromFile("/sys/class/power_supply/BAT0/energy_now", "/sys/class/power_supply/BAT0/charge_now");

if (powerNow == 0)
powerNow = 1;
Expand All @@ -83,7 +83,7 @@ public List<Battery> GetBatteryList()
{
DesignCapacity = designCapacity,
FullChargeCapacity = fullChargeCapacity,
BatteryStatusDescription = TryReadFileText("/sys/class/power_supply/BAT0/status"),
BatteryStatusDescription = TryReadTextFromFile("/sys/class/power_supply/BAT0/status"),

EstimatedChargeRemaining = (ushort)(energyNow * 100 / fullChargeCapacity), // current charge remaining in percentage
EstimatedRunTime = energyNow / powerNow, // current remaining life in minutes
Expand All @@ -106,9 +106,9 @@ public List<BIOS> GetBiosList()

BIOS bios = new BIOS
{
ReleaseDate = TryReadFileText("/sys/class/dmi/id/bios_date"),
Version = TryReadFileText("/sys/class/dmi/id/bios_version"),
Manufacturer = TryReadFileText("/sys/class/dmi/id/bios_vendor")
ReleaseDate = TryReadTextFromFile("/sys/class/dmi/id/bios_date"),
Version = TryReadTextFromFile("/sys/class/dmi/id/bios_version"),
Manufacturer = TryReadTextFromFile("/sys/class/dmi/id/bios_vendor")
};

biosList.Add(bios);
Expand All @@ -120,7 +120,7 @@ public List<CPU> GetCpuList(bool includePercentProcessorTime = true)
{
List<CPU> cpuList = new List<CPU>();

string[] lines = TryReadFileLines("/proc/cpuinfo");
string[] lines = TryReadLinesFromFile("/proc/cpuinfo");

Regex vendorIdRegex = new Regex(@"^vendor_id\s+:\s+(.+)");
Regex modelNameRegex = new Regex(@"^model name\s+:\s+(.+)");
Expand Down Expand Up @@ -210,9 +210,9 @@ private static void GetCpuUsage(CPU cpu)
// cpu2 86902920 6411506 12412331 769921453 17877927 0 4809331 0 0
// ...

string[] cpuUsageLineLast = TryReadFileLines("/proc/stat");
string[] cpuUsageLineLast = TryReadLinesFromFile("/proc/stat");
Task.Delay(500).Wait();
string[] cpuUsageLineNow = TryReadFileLines("/proc/stat");
string[] cpuUsageLineNow = TryReadLinesFromFile("/proc/stat");

if (cpuUsageLineLast.Length > 0 && cpuUsageLineNow.Length > 0)
{
Expand Down Expand Up @@ -417,9 +417,9 @@ public List<Motherboard> GetMotherboardList()

Motherboard motherboard = new Motherboard
{
Product = TryReadFileText("/sys/class/dmi/id/board_name"),
Manufacturer = TryReadFileText("/sys/class/dmi/id/board_vendor"),
SerialNumber = TryReadFileText("/sys/class/dmi/id/board_serial")
Product = TryReadTextFromFile("/sys/class/dmi/id/board_name"),
Manufacturer = TryReadTextFromFile("/sys/class/dmi/id/board_vendor"),
SerialNumber = TryReadTextFromFile("/sys/class/dmi/id/board_serial")
};

motherboardList.Add(motherboard);
Expand Down Expand Up @@ -452,9 +452,9 @@ public override List<NetworkAdapter> GetNetworkAdapterList(bool includeBytesPers

foreach (NetworkAdapter networkAdapter in networkAdapterList)
{
List<string>? networkAdapterUsageLast = TryReadFileLines("/proc/net/dev").FirstOrDefault(l => l.Trim().StartsWith(networkAdapter.Name))?.Trim().Split(charSeparators, StringSplitOptions.RemoveEmptyEntries).ToList();
List<string>? networkAdapterUsageLast = TryReadLinesFromFile("/proc/net/dev").FirstOrDefault(l => l.Trim().StartsWith(networkAdapter.Name))?.Trim().Split(charSeparators, StringSplitOptions.RemoveEmptyEntries).ToList();
Task.Delay(1000).Wait();
List<string>? networkAdapterUsageNow = TryReadFileLines("/proc/net/dev").FirstOrDefault(l => l.Trim().StartsWith(networkAdapter.Name))?.Trim().Split(charSeparators, StringSplitOptions.RemoveEmptyEntries).ToList();
List<string>? networkAdapterUsageNow = TryReadLinesFromFile("/proc/net/dev").FirstOrDefault(l => l.Trim().StartsWith(networkAdapter.Name))?.Trim().Split(charSeparators, StringSplitOptions.RemoveEmptyEntries).ToList();

if (networkAdapterUsageLast != null && networkAdapterUsageLast.Count > 0 && networkAdapterUsageNow != null && networkAdapterUsageNow.Count > 0)
{
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Battery, BIOS, CPU - processor, storage drive, keyboard, RAM - memory, monitor,
1. Include NuGet package from https://www.nuget.org/packages/Hardware.Info

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

2. Call `RefreshAll()` or one of the other `Refresh*()` methods:
Expand Down Expand Up @@ -180,6 +180,8 @@ Setting `includeNetworkAdapterConfiguration` to `false` has only a small impact

## Version history:

- 10.0.0.1:
- Fixed `GetBatteryList()` in Linux - by [@Tadelsucht](https://github.com/Tadelsucht)
- 10.0.0.0:
- Fixed `GetDriveList()` and `GetMemoryList()` in Linux - thanks to [@misaka00251](https://github.com/misaka00251)
- 1.1.1.1:
Expand Down

0 comments on commit 05213aa

Please sign in to comment.