Files
linker/linker.libs/SystemIdHelper.cs
2024-10-01 22:38:02 +08:00

42 lines
2.0 KiB
C#

using linker.libs.extends;
using System;
namespace linker.libs
{
public static class SystemIdHelper
{
public static string GetSystemId()
{
return OperatingSystem.IsWindows() ? GetSystemIdWindows() : OperatingSystem.IsLinux() ? GetSystemIdLinux() : GetSystemIdOSX();
}
private static string GetSystemIdWindows()
{
string cpu = CommandHelper.Execute("wmic", "csproduct get UUID", [], out string error).TrimNewLineAndWhiteSapce().Split(Environment.NewLine)[1];
string username = CommandHelper.Execute("whoami", string.Empty, [], out error).TrimNewLineAndWhiteSapce().Trim();
return $"{cpu}↓{username}↓{System.Runtime.InteropServices.RuntimeInformation.OSDescription}";
}
private static string GetSystemIdLinux()
{
string cpu = CommandHelper.Linux(string.Empty, ["cat /sys/class/dmi/id/product_uuid"]).TrimNewLineAndWhiteSapce();
if (string.IsNullOrWhiteSpace(cpu) || cpu.Contains("No such file or directory"))
{
cpu = CommandHelper.Linux(string.Empty, ["dmidecode -s system-uuid"]).TrimNewLineAndWhiteSapce();
if (string.IsNullOrWhiteSpace(cpu) || cpu.Contains("dmidecode"))
{
LoggerHelper.Instance.Error(cpu);
return string.Empty;
}
}
string username = CommandHelper.Linux(string.Empty, ["whoami"]).TrimNewLineAndWhiteSapce();
return $"{cpu}↓{username}↓{System.Runtime.InteropServices.RuntimeInformation.OSDescription}";
}
private static string GetSystemIdOSX()
{
string cpu = CommandHelper.Osx(string.Empty, ["system_profiler SPHardwareDataType | grep \"Hardware UUID\""]).TrimNewLineAndWhiteSapce();
string username = CommandHelper.Osx(string.Empty, ["whoami"]).TrimNewLineAndWhiteSapce();
return $"{cpu.Trim()}↓{username}↓{System.Runtime.InteropServices.RuntimeInformation.OSDescription}";
}
}
}