Skip to content

Instantly share code, notes, and snippets.

@Kegelcizer
Kegelcizer / UserUtil.cs
Last active May 24, 2022 12:33
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));
}