Skip to content

Instantly share code, notes, and snippets.

@CrazyVoid
Created October 13, 2023 20:43
Show Gist options
  • Save CrazyVoid/f37ca96e4b208150bafc2a064bb670c5 to your computer and use it in GitHub Desktop.
Save CrazyVoid/f37ca96e4b208150bafc2a064bb670c5 to your computer and use it in GitHub Desktop.
Small platform class for detecting what platform you are on and getting info like cpu and ram by platform
using System;
using System.Diagnostics;
using System.IO;
using System.Management;
namespace Torchwood
{
public class PlatformInfo
{
public static string GetOperatingSystem()
{
PlatformID platform = Environment.OSVersion.Platform;
switch (platform)
{
case PlatformID.Win32NT:
case PlatformID.Win32S:
case PlatformID.Win32Windows:
case PlatformID.WinCE:
return "Windows";
case PlatformID.MacOSX:
return "macOS";
case PlatformID.Unix:
string linuxDistribution = GetLinuxDistribution();
if (!string.IsNullOrEmpty(linuxDistribution))
{
return linuxDistribution;
}
return "Linux";
default:
return "Unknown";
}
}
public static string GetLinuxDistribution()
{
if (File.Exists("/etc/os-release"))
{
string osRelease = File.ReadAllText("/etc/os-release");
if (osRelease.Contains("ID=ubuntu") || osRelease.Contains("ID_LIKE=debian"))
{
return "Ubuntu/Debian";
}
else if (osRelease.Contains("ID=centos"))
{
return "CentOS";
}
else if (osRelease.Contains("ID=fedora"))
{
return "Fedora";
}
// Add more checks for other distributions that support .NET Core
}
return null;
}
public static bool IsLinux(string inputOS)
{
if(inputOS == "Ubuntu/Debian" || inputOS == "CentOS" || inputOS == "Fedora")
{
return true;
}
return false;
}
public static bool IsWindows(string inputOS)
{
if(inputOS == "Windows")
{
return true;
}
return false;
}
public static bool IsMac(string inputOS)
{
if (inputOS == "macOS")
{
return true;
}
return false;
}
private static string GetMacCPUInfo()
{
Process process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "sysctl",
Arguments = "machdep.cpu.brand_string",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return output;
}
private static string GetWindowsCPUInfo()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
ManagementObjectCollection collection = searcher.Get();
foreach (ManagementObject obj in collection)
{
return obj["Name"].ToString(); // CPU name
}
return "CPU information not found.";
}
private static string GetLinuxCPUInfo()
{
string cpuInfoPath = "/proc/cpuinfo";
if (File.Exists(cpuInfoPath))
{
return File.ReadAllText(cpuInfoPath);
}
return "CPU information not found.";
}
public static string GetCPUInfo(string operatingSystem)
{
switch (operatingSystem)
{
case "Ubuntu/Debian":
case "CentOS":
case "Fedora":
return GetLinuxCPUInfo();
case "macOS":
return GetMacCPUInfo();
case "Windows":
return GetWindowsCPUInfo();
default:
return "Incompatible OS!";
}
}
private static string GetMacRamInfo()
{
Process process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "sysctl",
Arguments = "hw.memsize",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
if (!string.IsNullOrEmpty(output))
{
ulong memSize = ulong.Parse(output.Trim());
return $"{memSize / (1024 * 1024)} MB";
}
return "RAM information not found.";
}
private static string GetWindowsRamInfo()
{
ManagementObjectSearcher ramSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem");
ManagementObjectCollection ramCollection = ramSearcher.Get();
foreach (ManagementObject obj in ramCollection)
{
ulong totalRam = Convert.ToUInt64(obj["TotalPhysicalMemory"]);
return $"{totalRam / (1024 * 1024)} MB";
}
return "RAM information not found.";
}
private static string GetLinuxRamInfo()
{
// RAM info on Linux can be obtained from system files, e.g., /proc/meminfo.
string memInfoPath = "/proc/meminfo";
if (File.Exists(memInfoPath))
{
string totalRamLine = File.ReadLines(memInfoPath).FirstOrDefault(line => line.StartsWith("MemTotal:"));
if (totalRamLine != null)
{
string[] parts = totalRamLine.Split(' ');
ulong totalRam = Convert.ToUInt64(parts[parts.Length - 2]);
return $"Total RAM: {totalRam / 1024} MB";
}
}
return "RAM information not found.";
}
public static string GetRamInfo(string operatingSystem)
{
switch (operatingSystem)
{
case "Ubuntu/Debian":
case "CentOS":
case "Fedora":
return GetLinuxRamInfo();
case "macOS":
return GetMacRamInfo();
case "Windows":
return GetWindowsRamInfo();
default:
return "Incompatible OS!";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment