Last active
May 24, 2022 12:33
-
-
Save Kegelcizer/8bb76df4d6208aa1d8aa130fed09472c to your computer and use it in GitHub Desktop.
Find if user is in admin group
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
public static bool IsInAdminGroup(string userName = null) | |
{ | |
using (var d = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer")) | |
using (var g = d.Children.Find("Administrators", "group")) | |
{ | |
return (g.Invoke("Members", null) as IEnumerable) | |
.Cast<object>() | |
.Select(m => new DirectoryEntry(m)) | |
.Any(m => m.Name.Equals(userName ?? Environment.UserName)); | |
} | |
} | |
public static bool IsRunningAsAdmin() | |
{ | |
using (var identity = WindowsIdentity.GetCurrent()) | |
{ | |
return new WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment