Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Check if a user is an admin and if not try to elevate the application by restarting itself. When using the verb "runas" User Account Control will popup a dialog and ask for permission to include administrative rights or to login as an administrator.
public static string ProcessFilename
{
get { return Process.GetCurrentProcess().MainModule.FileName; }
}
private static bool HasAdministrativeRight()
{
WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
return windowsIdentity != null
&& new WindowsPrincipal(windowsIdentity).IsInRole(WindowsBuiltInRole.Administrator);
}
private static void RestartElevated(string command)
{
var processInfo = new ProcessStartInfo
{
Verb = "runas",
FileName = ProcessFilename,
Arguments = command
};
try
{
Process.Start(processInfo);
}
catch (Win32Exception)
{
//Do nothing. Probably the user canceled the UAC window
}
}
@fredrikhaglund

This comment has been minimized.

Copy link
Owner Author

@fredrikhaglund fredrikhaglund commented Jun 20, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment