查询计算机所在的域里的其他电脑或其他域用户 using System.DirectoryServices; namespace ADDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { IsInDomain("shanghai"); this.label1.Text = GetCurrentFullName(); } private string GetCurrentFullName() { string DomainName = System.Environment.UserDomainName; string AccountName = System.Environment.UserName.ToLower(); DirectoryEntry de = new DirectoryEntry("LDAP://" + DomainName); DirectorySearcher ds = new DirectorySearcher(de); ds.Filter = ("(objectClass=user)"); foreach (SearchResult sr in ds.FindAll()) { string fullName = sr.GetDirectoryEntry().Name.ToString(); if (sr.GetDirectoryEntry().Properties["samaccountname"].Value.ToString().ToLower() == AccountName) { return fullName.Substring(3, fullName.Length - 3); } } return ""; } private bool IsInDomain(string domainName) { return IsInDomain(domainName, Environment.MachineName); } private bool IsInDomain(string domainName, string machineName) { DirectoryEntry de = new DirectoryEntry("LDAP://" + domainName); DirectorySearcher ds = new DirectorySearcher(de); ds.Filter = ("(objectClass=computer)"); foreach (SearchResult sr in ds.FindAll()) { string fullName = sr.GetDirectoryEntry().Name.ToString(); Console.WriteLine(fullName); if (sr.GetDirectoryEntry().Properties["samaccountname"].Value.ToString().ToLower() == machineName.ToLower()) { return true;// fullName.Substring(3, fullName.Length - 3); } } return false; } } }