62 lines
2.4 KiB
C#
62 lines
2.4 KiB
C#
using Store.Management.Framework.Configs;
|
|
using System.Diagnostics;
|
|
using System.Reflection;
|
|
using System.Security;
|
|
using System.Security.Principal;
|
|
|
|
namespace Store.Management.Framework;
|
|
|
|
/// <summary>当前进程的应用信息;目录与持久化设置分别由 FolderConfig 和设置服务管理。</summary>
|
|
public static class Config
|
|
{
|
|
public static Guid SessionID { get; } = Guid.NewGuid();
|
|
|
|
/// <summary>进程启动时间(本地时间),不受首次访问 Config 的时机影响。</summary>
|
|
public static DateTime AppStartTime { get; } = GetAppStartTime();
|
|
|
|
public static string AppName => FolderConfig.AppName;
|
|
public static string AppFullName => AppName;
|
|
public static Version WindowsVersion => Environment.OSVersion.Version;
|
|
|
|
/// <summary>优先使用入口程序集文件版本;单文件发布等场景回退到程序集版本。</summary>
|
|
public static Version Version { get; } = GetVersion();
|
|
|
|
/// <summary>入口程序集的编译配置,不使用是否附加调试器作为判断依据。</summary>
|
|
public static bool IsRelease { get; } = !string.Equals(
|
|
EntryAssembly.GetCustomAttribute<AssemblyConfigurationAttribute>()?.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<AssemblyFileVersionAttribute>()?.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;
|
|
}
|
|
}
|