• We just launched and are currently in beta. Join us as we build and grow the community.

Get IP Address of a Website using C#

Denny

Meme Genius
D Rep
0
0
0
Rep
0
D Vouches
0
0
0
Vouches
0
Posts
137
Likes
191
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial, i will teach you how to create a program that gets an IP address of a website using C#. When a user inputs the link of a particular website, then the program determines its ip address.

So, now let's start this tutorial!

1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your program IP Address of a Host.

2. Next, add a TextBox named TextBox1 for the inputting of a website and TextBox2 for displaying its IP address. Insert also a Button named Button1 that will trigger to get the IP address of the website inputted. You must design your interface like this:

ipdesign.png


3. Import System.Net namespace because this library has the internet and network provider.

  1. using

    System.Diagnostics

    ;
  2. using

    System

    ;
  3. using

    System.Windows.Forms

    ;
  4. using

    System.Collections

    ;
  5. using

    System.Drawing

    ;
  6. using

    System.Data

    ;
  7. using

    System.Collections.Generic

    ;


  8. using

    System.Net

    ;

4. Now, put this code in Button1_Click. This will trigger to determine the IP address of the site inputted.

Use the IPHostEntry Class to provide a class for Internet host address information and have its variable named hostname. Have its value by the Dns class (Domain Name Service) with GetHostByName method hold the inputted site in Textbox1.

  1. IPHostEntry hostname =

    Dns.

    GetHostByName

    (

    TextBox1.

    Text

    )

    ;

Now, create an IPAddress Class with variable name ip because this will provide an access for IP (Internet Protocol) addresses with the address list of your hostname variable of textbox1.

  1. IPAddress[

    ]

    ip =

    hostname.

    AddressList

    ;

Then display the output (IP address of the host) in textbox2 by converting the IP address to string.

  1. TextBox2.

    Text

    =

    (

    string

    )

    (

    ip[

    0

    ]

    .

    ToString

    (

    )

    )

    ;

Full source code:
  1. using

    System.Diagnostics

    ;
  2. using

    System

    ;
  3. using

    System.Windows.Forms

    ;
  4. using

    System.Collections

    ;
  5. using

    System.Drawing

    ;
  6. using

    System.Data

    ;
  7. using

    System.Collections.Generic

    ;


  8. using

    System.Net

    ;

  9. namespace

    IP_Address_of_Host
  10. {
  11. public

    partial

    class

    Form1
  12. {
  13. public

    Form1(

    )
  14. {
  15. InitializeComponent(

    )

    ;
  16. }

  17. public

    void

    Button1_Click(

    System

    .

    Object

    sender, System

    .

    EventArgs

    e)
  18. {
  19. IPHostEntry hostname =

    Dns.

    GetHostByName

    (

    TextBox1.

    Text

    )

    ;
  20. IPAddress[

    ]

    ip =

    hostname.

    AddressList

    ;
  21. TextBox2.

    Text

    =

    (

    string

    )

    (

    ip[

    0

    ]

    .

    ToString

    (

    )

    )

    ;
  22. }
  23. }
  24. }

Press F5 to run the program.

Output:

ipoutput.png


Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top