diff --git a/STranslate.Util/CommonUtil.cs b/STranslate.Util/CommonUtil.cs index 463ae214..27b39cd6 100644 --- a/STranslate.Util/CommonUtil.cs +++ b/STranslate.Util/CommonUtil.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; +using System.Security.Principal; using System.Text; using System.Windows; using System.Windows.Controls; @@ -398,6 +399,18 @@ public static System.Windows.Point GetMousePositionWindowsForms() return new System.Windows.Point(ms.X / dpiScale, ms.Y / dpiScale); } + /// + /// 是否为管理员权限 + /// + /// + public static bool IsUserAdministrator() + { + WindowsIdentity identity = WindowsIdentity.GetCurrent(); + WindowsPrincipal principal = new(identity); + + return principal.IsInRole(WindowsBuiltInRole.Administrator); + } + #endregion Other } } diff --git a/STranslate/App.xaml.cs b/STranslate/App.xaml.cs index f65c6eb6..b1ae88c8 100644 --- a/STranslate/App.xaml.cs +++ b/STranslate/App.xaml.cs @@ -7,7 +7,6 @@ using System.Diagnostics; using System.IO; using System.Reflection; -using System.Security.Principal; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; @@ -76,28 +75,13 @@ protected override void OnExit(ExitEventArgs e) private bool NeedAdministrator() { - // 初始化管理员缓存 - Current.Properties["admin"] = false; - // 加载配置 var isRole = Singleton.Instance.CurrentConfig?.NeedAdministrator ?? false; if (!isRole) return false; - // 更新管理员缓存 - bool role = IsUserAdministrator(); - Current.Properties["admin"] = role; - - return !role; - } - - private bool IsUserAdministrator() - { - WindowsIdentity identity = WindowsIdentity.GetCurrent(); - WindowsPrincipal principal = new WindowsPrincipal(identity); - - return principal.IsInRole(WindowsBuiltInRole.Administrator); + return !CommonUtil.IsUserAdministrator(); } private bool TryRunAsAdministrator() diff --git a/STranslate/ViewModels/NotifyIconViewModel.cs b/STranslate/ViewModels/NotifyIconViewModel.cs index 743f2a59..4ad82752 100644 --- a/STranslate/ViewModels/NotifyIconViewModel.cs +++ b/STranslate/ViewModels/NotifyIconViewModel.cs @@ -51,7 +51,7 @@ public NotifyIconViewModel() public void UpdateToolTip(string msg = "") { - _ = bool.TryParse(Application.Current.Properties["admin"]?.ToString(), out bool isAdmin); + bool isAdmin = CommonUtil.IsUserAdministrator(); string toolTipFormat = isAdmin ? "STranslate {0}\r\n[Administrator] #\r\n{1}" : "STranslate {0} #\r\n{1}"; diff --git a/STranslate/Views/MainView.xaml.cs b/STranslate/Views/MainView.xaml.cs index d3cedfa9..9421523d 100644 --- a/STranslate/Views/MainView.xaml.cs +++ b/STranslate/Views/MainView.xaml.cs @@ -96,7 +96,7 @@ protected override void OnSourceInitialized(EventArgs e) { Hide(); - _ = bool.TryParse(Application.Current.Properties["admin"]?.ToString(), out bool isAdmin); + bool isAdmin = CommonUtil.IsUserAdministrator(); string toolTipFormat = isAdmin ? "STranslate[Admin] {0} started" : "STranslate {0} started";