Check if user has administartor rights
In numerous cases it is essential to know if the user has administrator rights or not. A good example for this would be when we want to decide if we want to write a value in HKEY_LOCAL_MACHINE
which requires administrator rights or HKEY_CURRENT_USER
which doesn’t.
The following method will return true if the application is running under administration rights or false if its not.
1 2 3 4 5 6 | public static bool IsUserAdministrator() { WindowsIdentity user = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(user); return principal.IsInRole(WindowsBuiltInRole.Administrator); } |
Leave a Reply