Introduction

This article is not a technical overview or large discussion. It is like a collection of tips on
how you can get the IP address or host name of a machine. In the Win32 API this could be
accomplished using the NetWork API. And this is still true in the .NET framework.
The only difference is finding and understanding what namespace and class to use to accomplish
this task. In the .NET framework the NetWork API is available in the
System.Net namespace. The DNS class in the System.Net
namespace can be used to get the hostname of a machine or get the IP address if the hostname
is already known. The DNS class provides a simple domain name resolution
functionality. The DNS class is a static class that provides access to information
from the Internet Domain Name System (DNS). The information returned includes multiple IP
addresses and aliases if the host specified has more than one entry in the DNS database.
The list is returned as a collection or an array of IPAddress objects.
The following section is the code that shows how to obtain the IP address for a given host name.

What The Code Does

If you want to obtain the hostname of the local machine, then call the
GetHostName method without a parameter. Then use the resulting
hostname as a parameter to the GetHostByName method to get the
list of IPAddresses that may be associated with the hostname.
Then iterate through the collection of addresses to write out the IP Addresses
associated with the hostname.

Reminders

Make sure that you include the System.Net namespace in your code;
otherwise the compiler will not know where to look for the DNS class.
Also when you use VisualStudio.NET for creating the project, make sure that you have the
System.NET.Dll in your reference list. For more information on the
DNS class and System.Net namespace, please refer to
the online documentation for the .NET SDK.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.