|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using System.Security.Principal; |
| 5 | + |
| 6 | +namespace ContextMenuEditor.Utilities; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Helper class for detecting and requesting elevation (admin rights). |
| 10 | +/// Based on: https://anthonysimmon.com/programmatically-elevate-dotnet-app-on-any-platform/ |
| 11 | +/// </summary> |
| 12 | +public static class ElevationHelper |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Checks if the current process is running with administrator privileges. |
| 16 | + /// </summary> |
| 17 | + /// <returns>True if elevated, false otherwise.</returns> |
| 18 | + public static bool IsElevated() |
| 19 | + { |
| 20 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 21 | + { |
| 22 | + using var identity = WindowsIdentity.GetCurrent(); |
| 23 | + var principal = new WindowsPrincipal(identity); |
| 24 | + return principal.IsInRole(WindowsBuiltInRole.Administrator); |
| 25 | + } |
| 26 | + |
| 27 | + // On Linux/macOS, check if running as root (UID 0) |
| 28 | + // Not applicable for this Windows-only WPF app, but included for completeness |
| 29 | + return geteuid() == 0; |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Attempts to restart the current application with elevated privileges. |
| 34 | + /// Returns true if the restart was initiated, false if already elevated or failed. |
| 35 | + /// </summary> |
| 36 | + /// <param name="args">Command-line arguments to pass to the elevated instance.</param> |
| 37 | + /// <returns>True if relaunch initiated (caller should exit), false if already elevated.</returns> |
| 38 | + public static bool TryRelaunchElevated(string[] args) |
| 39 | + { |
| 40 | + if (IsElevated()) |
| 41 | + { |
| 42 | + return false; // Already elevated, no need to relaunch |
| 43 | + } |
| 44 | + |
| 45 | + try |
| 46 | + { |
| 47 | + var currentProcessPath = Environment.ProcessPath |
| 48 | + ?? throw new InvalidOperationException("Cannot determine current process path"); |
| 49 | + |
| 50 | + var startInfo = new ProcessStartInfo |
| 51 | + { |
| 52 | + UseShellExecute = true, |
| 53 | + FileName = currentProcessPath, |
| 54 | + Verb = "runas" // Windows UAC elevation |
| 55 | + }; |
| 56 | + |
| 57 | + // Pass along original command-line arguments |
| 58 | + foreach (var arg in args) |
| 59 | + { |
| 60 | + startInfo.ArgumentList.Add(arg); |
| 61 | + } |
| 62 | + |
| 63 | + Process.Start(startInfo); |
| 64 | + return true; // Successfully started elevated process, caller should exit |
| 65 | + } |
| 66 | + catch (System.ComponentModel.Win32Exception) |
| 67 | + { |
| 68 | + // User declined UAC prompt |
| 69 | + return false; |
| 70 | + } |
| 71 | + catch (Exception ex) |
| 72 | + { |
| 73 | + System.Diagnostics.Debug.WriteLine($"Failed to elevate: {ex.Message}"); |
| 74 | + return false; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // P/Invoke for Unix systems (not used in this Windows-only app, but included for reference) |
| 79 | + [DllImport("libc", SetLastError = true)] |
| 80 | + private static extern uint geteuid(); |
| 81 | +} |
0 commit comments