IP LookUp Program in .NET


This is an IP look up program that uses C# Windows Forms and IPHostEntry to resolve the DNS request. You enter the URL in the first box and press the Look Up button and the IP shows in the bottom box.

I created the controls, instead of using the form designer and in doing so the placement is not that great. But it should give a very basic example of creating your own controls and how the IPHostEntry works.

using System;
using System.IO;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
using System.Text;
class IPLookUp : Form
{
TextBox txtBox1;
TextBox txtBox2;
string address;
string IPaddress;
public IPLookUp()
{
Text = "IP Look Up";
BackColor = Color.White;
txtBox1 =
new TextBox();
txtBox1.Parent =
this;
txtBox1.Text = "Enter URL";
txtBox1.Location =
new Point(Font.Height * 6, Font.Height * 2);
Button btnlook =
new Button();
btnlook.Parent =
this;
btnlook.BackColor = SystemColors.ControlDark;
btnlook.Text = "Look UP";
btnlook.Location =
new Point(Font.Height * 7, Font.Height * 6);
btnlook.Click +=
new EventHandler(ButtonOnClick);
Label label =
new Label();
label.Parent =
this;
label.Text = "IP Address";
label.Location =
new Point(Font.Height * 6, Font.Height * 10);
txtBox2 =
new TextBox();
txtBox2.Parent =
this;
txtBox2.Text = " ";
txtBox2.Location =
new Point(Font.Height * 6, Font.Height * 12);
Button btnquit =
new Button();
btnquit.Parent =
this;
btnquit.BackColor = SystemColors.ControlDark;
btnquit.Text = "Quit";
btnquit.Location =
new Point(Font.Height * 7, Font.Height * 16);
btnquit.Click +=
new EventHandler(ButtonQuitOnClick);
}
public void ButtonOnClick(object sender, EventArgs ea)
{
address = txtBox1.Text;
GetIP(address);
}
private void GetIP(string address)
{
try
{
IPHostEntry IPHost = Dns.Resolve(address);
IPAddress[] addresses = IPHost.AddressList;
IPaddress = addresses[0].ToString();
txtBox2.Text = IPaddress;
}
catch(Exception exc)
{
MessageBox.Show(exc.ToString());
}
}
public void ButtonQuitOnClick(object sender, EventArgs ea)
{
Application.Exit();
}
public static void Main()
{
Application.Run(
new IPLookUp());
}
}

Up Next
    Ebook Download
    View all
    Learn
    View all