Created
June 9, 2023 18:03
-
-
Save 5cover/26da3e131b0cec3496a8d6ef1de24d95 to your computer and use it in GitHub Desktop.
Ensure admin privileges helper method. Restarts the application with admin privileges.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void EnsureAdminPrivileges() | |
{ | |
if (new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator)) | |
{ | |
return; | |
} | |
ProcessStartInfo processInfo = new(Environment.ProcessPath.NotNull()) | |
{ | |
UseShellExecute = true, | |
Verb = "runas" | |
}; | |
try | |
{ | |
_ = Process.Start(processInfo); | |
} | |
catch (Win32Exception) | |
{ | |
// User clicked "No" in the UAC prompt | |
} | |
Current.Shutdown(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment