// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
namespace Admin.NET.Core;
///
/// 操作系统平台帮助类
///
public static class OsPlatformHelper
{
///
/// 是否为 Unix 系统
///
public static bool IsUnixSystem { get; } = GetIsUnixSystem();
///
/// 操作系统平台类型
///
public static OSPlatform PlatformType { get; } = GetPlatformType();
///
/// 是否 Windows 平台
///
public static bool IsWindows => PlatformType == OSPlatform.Windows;
///
/// 是否 Linux 平台
///
public static bool IsLinux => PlatformType == OSPlatform.Linux;
///
/// 是否 macOS 平台
///
public static bool IsMacOs => PlatformType == OSPlatform.OSX;
///
/// 是否 FreeBSD 平台
///
public static bool IsFreeBsd => PlatformType == OSPlatform.FreeBSD;
///
/// 处理器信息
///
///
/// 推荐使用,默认有缓存
///
public static RuntimeInfo RuntimeInfos => Cache.Default.GetOrAdd("RuntimeInfos", _ => GetRuntimeInfo(), 60);
///
/// 收集运行时信息
///
public static RuntimeInfo GetRuntimeInfo()
{
try
{
var currentProcess = Process.GetCurrentProcess();
var runtimeInfo = new RuntimeInfo
{
OsName = GetOperatingSystemName(),
OsDescription = RuntimeInformation.OSDescription,
OsVersion = Environment.OSVersion.Version.ToString(),
OsArchitecture = RuntimeInformation.OSArchitecture.ToString(),
ProcessArchitecture = RuntimeInformation.ProcessArchitecture.ToString(),
FrameworkDescription = RuntimeInformation.FrameworkDescription,
RuntimeVersion = Environment.Version.ToString(),
DatabaseType = App.GetOptions().ConnectionConfigs[0].DbType.ToString(),
Is64BitOperatingSystem = Environment.Is64BitOperatingSystem,
Is64BitProcess = Environment.Is64BitProcess,
IsInteractive = Environment.UserInteractive,
ProcessorCount = Environment.ProcessorCount,
SystemDirectory = Environment.SystemDirectory,
CurrentDirectory = Environment.CurrentDirectory,
MachineName = Environment.MachineName,
UserName = Environment.UserName,
UserDomainName = Environment.UserDomainName,
WorkingSet = Environment.WorkingSet,
SystemStartTime = DateTime.Now - TimeSpan.FromMilliseconds(Environment.TickCount64),
SystemUptime = TimeSpan.FromMilliseconds(Environment.TickCount64),
ProcessStartTime = currentProcess.StartTime,
ProcessUptime = DateTime.Now - currentProcess.StartTime,
ProcessId = currentProcess.Id,
ProcessName = currentProcess.ProcessName,
ClrVersion = Environment.Version.ToString(),
EnvironmentVariableCount = Environment.GetEnvironmentVariables().Count,
CommandLineArgs = Environment.GetCommandLineArgs()
};
return runtimeInfo;
}
catch (Exception ex)
{
// 异常时返回基本信息
return new RuntimeInfo
{
OsName = "Unknown",
OsDescription = $"Error collecting info: {ex.Message}"
};
}
}
///
/// 获取环境变量
///
/// 变量名
/// 环境变量目标
/// 环境变量值
public static string? GetEnvironmentVariable(string variableName, EnvironmentVariableTarget target = EnvironmentVariableTarget.Process)
{
return Environment.GetEnvironmentVariable(variableName, target);
}
///
/// 获取所有环境变量
///
/// 环境变量目标
/// 环境变量字典
public static Dictionary GetAllEnvironmentVariables(EnvironmentVariableTarget target = EnvironmentVariableTarget.Process)
{
var variables = Environment.GetEnvironmentVariables(target);
return variables.Cast()
.ToDictionary(entry => entry.Key.ToString()!, entry => entry.Value?.ToString());
}
///
/// 检查特定的操作系统版本
///
/// 最小版本要求
/// 是否满足版本要求
public static bool CheckOsVersion(Version minimumVersion)
{
try
{
return Environment.OSVersion.Version >= minimumVersion;
}
catch
{
return false;
}
}
///
/// 获取.NET运行时位置
///
/// 运行时路径
public static string GetRuntimeLocation()
{
try
{
return Path.GetDirectoryName(typeof(object).Assembly.Location) ?? string.Empty;
}
catch
{
return string.Empty;
}
}
///
/// 判断当前操作系统是否为 Unix 系统
///
///
public static bool GetIsUnixSystem()
{
return RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ||
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD);
}
///
/// 获取操作系统平台类型
///
private static OSPlatform GetPlatformType()
{
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? OSPlatform.Windows
: RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
? OSPlatform.Linux
: RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
? OSPlatform.OSX
: RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD)
? OSPlatform.FreeBSD
: OSPlatform.Create("Unknown");
}
///
/// 获取操作系统名称
///
private static string GetOperatingSystemName()
{
var platformType = GetPlatformType();
return platformType.ToString() switch
{
"Windows" => "Windows",
"Linux" => "Linux",
"OSX" => "MacOS",
"FreeBSD" => "FreeBSD",
_ => "Unknown"
};
}
}
///
/// 系统运行时信息
///
public record RuntimeInfo
{
///
/// 操作系统名称
///
public string OsName { get; set; } = string.Empty;
///
/// 操作系统描述
///
public string OsDescription { get; set; } = string.Empty;
///
/// 操作系统版本
///
public string OsVersion { get; set; } = string.Empty;
///
/// 操作系统架构
///
public string OsArchitecture { get; set; } = string.Empty;
///
/// 进程架构
///
public string ProcessArchitecture { get; set; } = string.Empty;
///
/// 运行时框架描述
///
public string FrameworkDescription { get; set; } = string.Empty;
///
/// 运行时版本
///
public string RuntimeVersion { get; set; } = string.Empty;
///
/// 数据库类型
///
public string DatabaseType { get; set; } = string.Empty;
///
/// 是否64位操作系统
///
public bool Is64BitOperatingSystem { get; set; }
///
/// 是否64位进程
///
public bool Is64BitProcess { get; set; }
///
/// 是否交互模式
///
public bool IsInteractive { get; set; }
///
/// 交互模式描述
///
public string InteractiveMode => IsInteractive ? "交互运行" : "非交互运行";
///
/// 处理器数量
///
public int ProcessorCount { get; set; }
///
/// 系统目录
///
public string SystemDirectory { get; set; } = string.Empty;
///
/// 当前目录
///
public string CurrentDirectory { get; set; } = string.Empty;
///
/// 机器名称
///
public string MachineName { get; set; } = string.Empty;
///
/// 用户名
///
public string UserName { get; set; } = string.Empty;
///
/// 用户域名
///
public string UserDomainName { get; set; } = string.Empty;
///
/// 工作集大小(字节)
///
public long WorkingSet { get; set; }
///
/// 系统启动时间
///
public DateTime SystemStartTime { get; set; }
///
/// 系统运行时间
///
public TimeSpan SystemUptime { get; set; }
///
/// 进程启动时间
///
public DateTime ProcessStartTime { get; set; }
///
/// 进程运行时间
///
public TimeSpan ProcessUptime { get; set; }
///
/// 进程ID
///
public int ProcessId { get; set; }
///
/// 进程名称
///
public string ProcessName { get; set; } = string.Empty;
///
/// CLR版本
///
public string ClrVersion { get; set; } = string.Empty;
///
/// 环境变量数量
///
public int EnvironmentVariableCount { get; set; }
///
/// 命令行参数
///
public string[] CommandLineArgs { get; set; } = [];
}