using Store.Management.Framework.Configs;
using System.Diagnostics;
using System.Reflection;
using System.Security;
using System.Security.Principal;
namespace Store.Management.Framework;
/// 当前进程的应用信息;目录与持久化设置分别由 FolderConfig 和设置服务管理。
public static class Config
{
public static Guid SessionID { get; } = Guid.NewGuid();
/// 进程启动时间(本地时间),不受首次访问 Config 的时机影响。
public static DateTime AppStartTime { get; } = GetAppStartTime();
public static string AppName => FolderConfig.AppName;
public static string AppFullName => AppName;
public static Version WindowsVersion => Environment.OSVersion.Version;
/// 优先使用入口程序集文件版本;单文件发布等场景回退到程序集版本。
public static Version Version { get; } = GetVersion();
/// 入口程序集的编译配置,不使用是否附加调试器作为判断依据。
public static bool IsRelease { get; } = !string.Equals(
EntryAssembly.GetCustomAttribute()?.Configuration,
"Debug", StringComparison.OrdinalIgnoreCase);
public static bool IsAdministrator
{
get
{
if (!OperatingSystem.IsWindows()) return false;
try
{
using WindowsIdentity identity = WindowsIdentity.GetCurrent();
return new WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator);
}
catch (SecurityException) { return false; }
catch (UnauthorizedAccessException) { return false; }
}
}
private static Assembly EntryAssembly => Assembly.GetEntryAssembly() ?? typeof(Config).Assembly;
public static string GetFullVersionString() =>
$"{Version} - {(IsRelease ? "Release" : "Debug")}{(IsAdministrator ? " - 管理员" : string.Empty)}";
private static Version GetVersion()
{
string? fileVersion = EntryAssembly.GetCustomAttribute()?.Version;
return System.Version.TryParse(fileVersion, out Version? version)
? version : EntryAssembly.GetName().Version ?? new Version(0, 0, 0, 0);
}
private static DateTime GetAppStartTime()
{
using Process process = Process.GetCurrentProcess();
return process.StartTime;
}
}