diff --git a/Business/Program.cs b/Business/Program.cs index 1a40dc1..a4e4fed 100644 --- a/Business/Program.cs +++ b/Business/Program.cs @@ -23,6 +23,7 @@ namespace SystemTrayMenu Config.SetFolderByWindowsContextMenu(args); Config.LoadOrSetByUser(); Config.Initialize(); + PrivilegeChecker.Initialize(); if (SingleAppInstance.Initialize()) { diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 38e476f..fded143 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -39,5 +39,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.3.2.4")] -[assembly: AssemblyFileVersion("1.3.2.4")] +[assembly: AssemblyVersion("1.3.2.6")] +[assembly: AssemblyFileVersion("1.3.2.6")] diff --git a/Utilities/Log.cs b/Utilities/Log.cs index ad4015b..2d5a998 100644 --- a/Utilities/Log.cs +++ b/Utilities/Log.cs @@ -159,8 +159,15 @@ namespace SystemTrayMenu.Utilities try { + string verb = string.Empty; + if (!PrivilegeChecker.IsCurrentUserInAdminGroup) + { + verb = "runas"; + } + using Process p = new() { + StartInfo = new ProcessStartInfo(fileName) { FileName = fileName, @@ -168,7 +175,7 @@ namespace SystemTrayMenu.Utilities WorkingDirectory = workingDirectory, CreateNoWindow = createNoWindow, UseShellExecute = true, - Verb = "runas", + Verb = verb, }, }; p.Start(); diff --git a/Utilities/PrivilegeChecker.cs b/Utilities/PrivilegeChecker.cs new file mode 100644 index 0000000..b2e639d --- /dev/null +++ b/Utilities/PrivilegeChecker.cs @@ -0,0 +1,29 @@ +// +// Copyright (c) PlaceholderCompany. All rights reserved. +// + +namespace SystemTrayMenu.Utilities +{ + using System.Linq; + using System.Security.Principal; + + internal static class PrivilegeChecker + { + public static bool IsCurrentUserInAdminGroup { get; set; } + + public static void Initialize() + { + // https://stackoverflow.com/questions/3600322/check-if-the-current-user-is-administrator + // https://learn.microsoft.com/en-us/troubleshoot/windows-server/identity/security-identifiers-in-windows + // S-1-5-32-544 + // A built-in group. After the initial installation of the operating system, + // the only member of the group is the Administrator account. + // When a computer joins a domain, the Domain Admins group is added to + // the Administrators group. When a server becomes a domain controller, + // the Enterprise Admins group also is added to the Administrators group. + var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); + var claims = principal.Claims; + IsCurrentUserInAdminGroup = claims.FirstOrDefault(c => c.Value == "S-1-5-32-544") != null; + } + } +} \ No newline at end of file