Retrieving the Domain Name and Host Name with VB.NET



Question: My Company is in the process of building an application that needs to retrieve the machine’s domain name and host name for use within the application. I have been looking around and this seems to be a bit more complex than I had thought. Any ideas how I might be able to do this?


Answer: Sure the System.NET.NetworkInformation namespace within the Framework 2.0 introduces some great new features to collect this information. This namespace can be used to access network traffic data, network address information and notification of address changes for the local computer. For what you are looking to do, you would want to take advantage of the IPGlobalProperties.


For example within a console application you could enter the following code.


Imports System.Net.NetworkInformation
Module Module1
    Sub Main()
      
Dim ipproperties As IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties()
      
Console.WriteLine("Domain Name:" & ipproperties.DomainName)
      
Console.WriteLine("Host Name:" & ipproperties.HostName)
End Sub
End Module


When this is run it would retrieve the information that you are looking for as shown below.


Skip to main content