How do I get the first name and last name of the logged in Windows user?

前端 未结 6 1026
独厮守ぢ
独厮守ぢ 2020-12-04 15:53

How I can get my first name last name with c# in my system (logging in windows with Active Directory username and pass)?

Is it possible to do that without going to t

6条回答
  •  既然无缘
    2020-12-04 16:22

    This solution didn't work for me but this function worked great:

    public static string GetUserFullName(string domain, string userName)
            {
                DirectoryEntry userEntry = new DirectoryEntry("WinNT://" + domain + "/" + userName + ",User");
                return (string)userEntry.Properties["fullname"].Value;
            }
    

    You should call it that way:

    GetUserFullName(Environment.UserDomainName, Environment.UserName);
    

    (Found it here).

提交回复
热议问题