Get Domain name of the system
System Name or NetBIOS Name
//Retrieve the NetBIOS name.
System.Environment.MachineName;
System.Net.Dns.GetHostName();
DNS name or Fully Qualified name
//It retrieves the domain name of the current user
System.Windows.Forms.SystemInformation.UserDomainName;
System.Environment.GetEnvironmentVariable("USERDNSDOMAIN"));
//Retrieve the DNS name of the computer.
System.Net.Dns.GetHostByName("LocalHost").HostName;
//WMI Script to retireve only domain name of current computer
ManagementPath path = new ManagementPath();
path.Server = System.Net.Dns.GetHostName();
path.NamespacePath = @"root\CIMV2";
path.RelativePath = "Win32_ComputerSystem.Name='" + path.Server + "'";
System.Management.ManagementObject o = new ManagementObject(path);
Console.WriteLine(o["Domain"]);
//Read domain and host name from windows registry in VB.NET
Dim rk As RegistryKey = Registry.LocalMachine
Dim rkSub As RegistryKey
rkSub = rk.OpenSubKey("SYSTEM\CurrentControlSet\Services\Tcpip\Parameters")
If rkSub Is Nothing Then
MessageBox.Show("Program error, invalid registry key", "Error", MessageBoxButtons.OK,MessageBoxIcon.Error)
Exit Sub
End If
mDomain = CType(rkSub.GetValue("Domain"), String)
mHostName = CType(rkSub.GetValue("HostName"), String)
If mDomain.Length > 0 Then
mFullHostName = mHostName & "." & mDomain
Else
mFullHostName = mHostName
End If