C#获取域帐户全名

不知道有没有一个属性直接获取,无奈写了这么一个函数

 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 "";  
    }

posted on 2008-05-20 17:13  catvi  阅读(837)  评论(0编辑  收藏  举报

导航