using (DirectoryEntry comp = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer")) { int AccountCount = 0; foreach (DirectoryEntry child in comp.Children) { //獲取賬戶名稱 if (child.SchemaClassName == "User") { using (DirectoryEntry NewUser = comp.Children.Find(child.Name, "user")) { //判斷該賬戶是否被禁用 bool AccountState = Convert.ToBoolean(NewUser.InvokeGet("AccountDisabled")); if (!AccountState) { AccountCount += 1; } } } } if (AccountCount > 1) { return 0; } else { return 1; } }
參考msdn:https://msdn.microsoft.com/zh-cn/library/aa746340(v=VS.85).aspx