The code below will return all users on the machine that are not marked as disabled. Be sure to add a reference to System.DirectoryServices.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | using System; using System.Collections.Generic; using System.DirectoryServices; //... private const int UF_ACCOUNTDISABLE = 0x0002; private List<DirectoryEntry> GetActiveMachineUsers() { List<DirectoryEntry> returnValue = new List<DirectoryEntry>(); DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName); foreach (DirectoryEntry user in localMachine.Children) { if (user.SchemaClassName == "User") { if (((int)user.Properties["UserFlags"].Value & UF_ACCOUNTDISABLE) != UF_ACCOUNTDISABLE) { returnValue.Add(user); } } } return returnValue; } |