Page 1 of 1

Host Name To IP Updated Get IP From Hostname

#1 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Posted 12 April 2009 - 04:23 AM

:D
A Small Application Hostname To IP in C#
Posted Image

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
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.

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...)
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.

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.

  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

				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;
			}

		}

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Host Name To IP Updated

#2 dana sorani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-March 14

Posted 06 March 2014 - 11:28 AM

Great Tutorial But I Will Reach Some Problem In This Statement In The Method GetLocalIP



string _LocalIP = Dns.GetHostName();


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1