Machine's domain name in .NET?

前端 未结 6 1118
小蘑菇
小蘑菇 2021-02-03 22:47

There gotta be an easy way to do this, I can\'t believe there\'s none. I have scanned through net and found, like, 20 different methods to find in which domain current user is,

6条回答
  •  青春惊慌失措
    2021-02-03 22:57

    Using GetCurrentDomain is the same as Environment.UserDomainName, which works incorrectly if your program is running on a domain computer as a non-domain user. I've used the following code:

    try
    {
        return System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain().Name;
    }
    catch (Exception)
    {
        return Environment.UserDomainName;
    }
    

提交回复
热议问题