Fix NullReferenceException on linux systems where the specified paths don't exist (#1300)
* feat(SMBios): Handle null reference exception when accessing Board information Changed the ReadSysFs method in the class SMBios to return an empty string instead of null when unable to find the needed paths on Linux, preventing NullReference Exception when getting the manufacturer and model in the Motherboard class. * fix(LMSensors): Prevent exception in LMSensors constructor Introduced string array subDirectories which is initialized empty. Added checking if the hwmon directory exists otherwise the array subDirectories will stay empty so it does not throw an exception if it can't find the path. * Simplify. --------- Co-authored-by: PhyxionNL <7643972+PhyxionNL@users.noreply.github.com>
This commit is contained in:
co-authored by
PhyxionNL
parent
dc9792fc9f
commit
8ab4def93a
@@ -16,17 +16,21 @@ namespace LibreHardwareMonitor.Hardware.Motherboard.Lpc;
|
||||
|
||||
internal class LMSensors
|
||||
{
|
||||
private readonly List<ISuperIO> _superIOs = new();
|
||||
private const string HwMonPath = "/sys/class/hwmon/";
|
||||
private readonly List<ISuperIO> _superIOs = [];
|
||||
|
||||
public LMSensors()
|
||||
{
|
||||
foreach (string basePath in Directory.GetDirectories("/sys/class/hwmon/"))
|
||||
if (!Directory.Exists(HwMonPath))
|
||||
return;
|
||||
|
||||
foreach (string basePath in Directory.GetDirectories(HwMonPath))
|
||||
{
|
||||
foreach (string devicePath in new[] { "/device", string.Empty })
|
||||
{
|
||||
string path = basePath + devicePath;
|
||||
|
||||
string name = null;
|
||||
|
||||
try
|
||||
{
|
||||
using StreamReader reader = new(path + "/name");
|
||||
|
||||
@@ -1324,11 +1324,11 @@ public class SMBios
|
||||
return reader.ReadLine();
|
||||
}
|
||||
|
||||
return null;
|
||||
return string.Empty;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user