Pages

Monday, 31 March 2014

How to get IP Address using C#


Hello All,

Here's a tutorial to get your IP Address using C#. 




N.B: Beginners please follow the link to know How to create your First Windows Form Application.


Steps

1. Open Visual Studio -> New Project -> Windows Forms Application. Give name as
    IP Address and press OK. (See the Screenshot)




2. Now design your Form as shown in the below image. (Change the Form
   backcolor and Form text from the Properties window to make your form same as 
   the one shown below)


3. Now click on the Form and get the event handler for Form Load and update the code 
    with the one shown below.

Form1.cs

using System;
using System.Net;
using System.Windows.Forms;

namespace IP_Address
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            getIP();
        }
        void getIP()
        {
            string localIP = string.Empty;
            IPHostEntry host;

            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    localIP = ip.ToString();
                }
            }
            textBox1.Text = localIP;

        }
    }

}

4. Run your Application and get your form with your IP Address. In case of any errors / 
    doubts, please feel free to comment; I am happy to help you.


See Also





1 comment:

  1. Nice information. I had checked my External ip address using this site IP-Details.com

    ReplyDelete

 

About Me

I am Tijo Tom, a Computer Engineer from India.Right from my schooling, I had a passion for Programming, PC Tricks, Virus Creation and Hacking.

I started this blog on Jan 1, 2014 to share my views and ideas with the world.

You can know more about me on