Skip to content

Instantly share code, notes, and snippets.

@Kegelcizer
Last active May 24, 2022 12:33
Show Gist options
  • Save Kegelcizer/8bb76df4d6208aa1d8aa130fed09472c to your computer and use it in GitHub Desktop.
Save Kegelcizer/8bb76df4d6208aa1d8aa130fed09472c to your computer and use it in GitHub Desktop.
Find if user is in admin group
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