Reputation: 3990
I am trying to get domain name using C# with the below code,
static void Main(string[] args)
{
var domainName = IPGlobalProperties.GetIPGlobalProperties().DomainName;
Console.WriteLine(domainName);
Console.ReadLine();
}
It prints domain name correctly in domain connected windows 8 machine. But it doesn't print anything in windows server 2012 R2 machine.
How to get domain name in Windows server 2012 machine.
Upvotes: 2
Views: 1455
Reputation: 597
In the past, I have used this code to get the domain name. It is accessed through the environmental variables on that machine.
string UserDomain = Environment.UserDomainName.ToString();
Console.WriteLine(UserDomain);
Perhaps the exception is caused from running it on a server for which the user does not have the appropriate permissions. Have you tried doing a "run As administrator" or "run as different user" with the exe?
Upvotes: 1
Reputation: 1441
I hope that works for you:
string domainName = System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain().Name;
Upvotes: 1