Get IP Address of a Website using C#

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: output 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.  
  9.  
  10. 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.  
  9.  
  10. using System.Net;
  11.  
  12. namespace IP_Address_of_Host
  13. {
  14. public partial class Form1
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. }
  20.  
  21. public void Button1_Click(System.Object sender, System.EventArgs e)
  22. {
  23. IPHostEntry hostname = Dns.GetHostByName(TextBox1.Text);
  24. IPAddress[] ip = hostname.AddressList;
  25. TextBox2.Text = (string) (ip[0].ToString());
  26. }
  27. }
  28. }
Press F5 to run the program. Output: output 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

Add new comment