Skip to content

Instantly share code, notes, and snippets.

@5cover
Created June 9, 2023 18:03
Show Gist options
  • Save 5cover/26da3e131b0cec3496a8d6ef1de24d95 to your computer and use it in GitHub Desktop.
Save 5cover/26da3e131b0cec3496a8d6ef1de24d95 to your computer and use it in GitHub Desktop.
Ensure admin privileges helper method. Restarts the application with admin privileges.
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