How do I programmatically check to see what domain I am connected to?
c#, dns, networking
Solution
Have you tried:
Environment.UserDomainName
You could also take a look at the active IP addresses on the machine, and query for one that works on your local network...
var x = NetworkInterface.GetAllNetworkInterfaces()
.Where(ni => ni.OperationalStatus == OperationalStatus.Up)
.SelectMany(ni => ni.GetIPProperties().UnicastAddresses);
// do something with the collection here to determine if you're on the right network.
// just looping & printing here for example.
foreach (var item in x)
{
Console.WriteLine(item.Address);
}
And after you've figured out the network that you're on, you could subscribe to the System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged event to handle your computer jumping networks while your app is running.
Problem
If I'm connected to the local LAN here at work, I need to have my app access our server via an internal IP, otherwise, I'll need to use our external IP when out in the wild. Currently, I just try to connect via the local IP and then try the external if it fails... but the timeout takes a bit too long and I was wondering if there's a way to find out what domain the machine is connected to before trying. Edit: Patrick> Essentially, the app runs on a tablet pc that is connected to the local network a couple of times a day. It's roughly equal between the number of times it connects over the network and the times that it connects locally. All machines have a domain account when they are connected to the network (and have domain accounts with a naming convention of like "LOCTabletx" where x is a number given to the machine when it's ghosted. What I'm looking for is a fast way to see if the machine is connected on our local network or the internet. Using Environment.UserDomainName gets me LOCTabletx and not the domain name. EDIT If it helps anyone, I just try to DNS Resolve the name of a machine that I can guarantee will be on the network (one of the servers). It works sufficiently well for me.