1

I have a problem with a C# Application I am making. I am trying to get all IPs to display in a textbox within a windows form by simply clicking a button. I currently have a base to work with:

    private void btnIP_Click(object sender, EventArgs e)
    {
         NetworkInterface[] ipadapters = NetworkInterface.GetAllNetworkInterfaces();
        string iptemplate = @"
        Network adapter: {0}
        IP:              {1}";

        string IPText = "";

        foreach (NetworkInterface AdapterIP in ipadapters)
        {

            IPText = IPText + String.Format(iptemplate,
            AdapterIP.Name,
            AdapterIP = ipadd());

        }
            txtOutput.Text = IPText;
        }

However, I am just not understanding whether I can do this using the

using System.Net.NetworkInformation;

If someone can just explain to me how I can take IPs from there and display it, it would help a lot. I have seen people query DNS to retrieve a hostname etc, but I just want to show all IPv4 and IPv6 addresses for all adapters.

I am just not understanding it. I know that I need to keep the foreach() method but not sure how to build out this code to make it work. Note: I have it working for a descriptions and everything for all adapters but I've created a separate button solely dedicated to IPs.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Maddox
  • 19
  • 3
  • What bit don't you understand? This SO question goes into some detail: http://stackoverflow.com/questions/1069103/how-to-get-my-own-ip-address-in-c ...until you explain exactly what you don't understand. – Arran Dec 13 '12 at 19:09
  • I have done this before, but I did it for a CompactFramework application and used a lot of P/Invokes, I'm not sure if there exists a more managed way to do this. But, you are correct that you should **not** have to query DNS !! – Alan Dec 13 '12 at 19:10
  • Seems duplicate question http://stackoverflow.com/questions/5271724/get-all-ip-addresses-on-machine?rq=1 – Amitd Dec 13 '12 at 19:10

3 Answers3

1
var ipEntry = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in ipEntry.AddressList)
{
   if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
   {
    //IPv6
   }
   else if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
   {
    //IPv4
   }
}
d.moncada
  • 16,900
  • 5
  • 53
  • 82
  • This way it would only display 1 adapter (random). I want to build the whole adapter list and then display only the IP address including the name of the adapter and at the top hostname of the computer. – Maddox Dec 13 '12 at 19:29
  • @Maddox Are you using WPF or Winforms? And to confirm, you're looking to only display the local IP address/adapter name of the computer? – d.moncada Dec 13 '12 at 19:34
  • WinForms and I'm trying to list all the adapters on the local machine. Including: Name, Description and IP Address (v4, v6) of those adapters. – Maddox Dec 13 '12 at 19:42
1
string hostName = Dns.GetHostName();

string myIP = Dns.GetHostByName(hostName).AddressList[0].ToString();
Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
madhura
  • 41
  • 3
0

Below is a great post about how to do this. It provides all the info you should need.

How to get the IP address of the server on which my C# application is running on?

Basically it's this:

var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
    var localIP = ip.ToString();
    //do more here...
}
Community
  • 1
  • 1
jmrnet
  • 548
  • 3
  • 11