c#如何获取当前windows域账号

2025-02-27 23:12:26
推荐回答(2个)
回答1:

Environment.UserName就是当前用户名

回答2:

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 "";
}
纯COPY的,做个任务