简体   繁体   中英

how to find user full name in domain from service running with system account in c#

I have service running with system account I found below code which gives proper user name when run with user account and gives error when run with system account. could someone help how to check logged in user full name when service is running with system account.

private static string GetFullName()
{
    try
    {
        DirectoryEntry de = new DirectoryEntry("WinNT://" + Environment.UserDomainName + "/" + Environment.UserName);
        return de.Properties["fullName"].Value.ToString();
    }
    catch { return null; }
}

Answer: Instead of using Environment.UserDomainName + "/" + Environment.UserName I manually passed the domain and username and it is resolved.

You can get first name and last name separately and then can return as below,

private static string GetFullName()
{
    try
    {
        DirectoryEntry de = new DirectoryEntry("WinNT://" + Environment.UserDomainName + "/" + Environment.UserName);
        return de.Properties["FullName"].Value.ToString();
    }
    catch { return null; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM