Error handling for OSUtils.GetOs() (now TryGetOs())

This commit is contained in:
N00MKRAD
2021-05-03 12:48:57 +02:00
parent 52bfcd8c6b
commit 9dc732dcf6
2 changed files with 37 additions and 16 deletions

View File

@@ -137,21 +137,28 @@ namespace Flowframes.OS
}
}
public static string GetOs()
public static string TryGetOs()
{
string info = "";
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem"))
try
{
ManagementObjectCollection information = searcher.Get();
if (information != null)
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem"))
{
foreach (ManagementObject obj in information)
info = $"{obj["Caption"]} | {obj["OSArchitecture"]}";
}
ManagementObjectCollection information = searcher.Get();
info = info.Replace("NT 5.1.2600", "XP").Replace("NT 5.2.3790", "Server 2003");
if (information != null)
{
foreach (ManagementObject obj in information)
info = $"{obj["Caption"]} | {obj["OSArchitecture"]}";
}
info = info.Replace("NT 5.1.2600", "XP").Replace("NT 5.2.3790", "Server 2003");
}
}
catch (Exception e)
{
Logger.Log("TryGetOs Error: " + e.Message, true);
}
return info;