A Small Application Hostname To IP in C#

In this tutorial I will be showing you how you would go about Getting IP from Website or Domain Name in C#. Reading and knowing this Project is very Simple, before we get into any code, you need to understand that this tutorial is not to advanced tutorial, using System.Net namespace. If you are not familiar with this item, I suggest you study them before attempting this tutorial.
Getting IP from Hostname is not so advanced but it is in intermediate level. This tutorial is not to long it is too short. And so this tutorial is not taking you time.
I will start by showing the Getting IP Function, Passing tow parameters.
So Let’s Began:
First of All Import The namespace
CODE
using System.Net;
The System.Net namespace provides a simple programming interface for many of the rotocols used on networks today. The WebRequest and WebResponse classes form the basis of what are called pluggable protocols, an implementation of network services that enables you to develop applications that use Internet resources without worrying about the specific details of the individual protocols.
Create the Global variables
Provides a container class for Internet host address information.
Provides an Internet Protocol (IP) address.
CODE
public IPHostEntry _IPHostEntry;
public IPAddress[] _IPAddress;
So let’s Create our function GetIP
System.Enviroment namespace is used to get information from computer
ex (Computer Name, UserName, UserDomainName etc...)
CODE
private static string GetIP (String _HostName,String _Return)
{
try
{
String myName = System.Environment.UserName;
IPHostEntry _IPHostEntry;
IPAddress[] _IPAddress;
if (_HostName == "" || _HostName == null)
{
_HostName = “localhost”
}
if (!_HostName.Contains("@") || !_HostName .Contains(".com"))
{
if (_HostName != myName )
{
_HostName = "www." + _HostName.ToLower() + ".com";
}
if (_HostName.ToLower() == myName.ToLower() || _HostName.ToLower() == "localhost")
{
{
_HostName = "localhost";
}
}
_IPHostEntry = Dns.GetHostByName(_HostName);
_IPAddress = _IPHostEntry.AddressList;
for (int i = 0; i < _IPAddress.Length; i++)
{
_Return = _IPAddress[0].ToString();
}
}
return _Return;
}
catch (Exception ex)
{
_Return = "";
String StrErr;
StrErr = ex.Message;
if (StrErr.Length > 35)
{
StrErr = StrErr.Substring(0, 35) + "...";
}
return StrErr;
}
}
And then get our computer information
Provides information about, and means to manipulate,the current environment and platform. This class cannot be inherited.
CODE
private void ComInfo()
{
Username = System.Environment.UserName.ToString();
ComName = System.Environment.MachineName.ToString();
//set username and computer name lables
lblcomname.Text = ComName;
lbluname.Text = Username;
}
Get our local And WAN ips
Dnd.GetHostName() Provides simple domain name resolution functionality.
CODE
private void GetLocalIP()
{
try
{
string _LocalIP = Dns.GetHostName();
_IPHostEntry = Dns.GetHostByName(_LocalIP);
_IPAddress = _IPHostEntry.AddressList;
for (int i = 0; i < _IPAddress.Length; i++)
{
lbllocalip.Text = _IPAddress[0].ToString();
}
download in from the target location it means that first it runs the page then download the information or ip and inisalize it to a string
CODE
String WanIP;
WanIP = new System.Net.WebClient().DownloadString(("http://www.whatismyip.com/automation/n09230945.asp"));
lbllocalip.Text = lbllocalip.Text + " | " + WanIP.ToString();
}
catch (Exception ex)
{
lblstatus.Text = ex.Message;
}
}