Share via


C# get local IP but IPAddress.AddressFamily has many IPs

Question

Saturday, November 1, 2014 2:39 PM

Hey, honor Guys,
I am very new to C#. And I met a question, that is, I wanna get local IP of my own computer, but when I use the following codes, I met some Problems. I don't know how to get the current using IP for me(Which IP am I using now!!)
(In this case, the 192.168.0.102 is my pc IP, the other two are for vmWares virtual machines IPs.)Who can help me???Thank you very much sincerely!
public class LocalIPCheck
    {
        public string LocalIP { get; private set; }

        public void CheckCurrentIP()
        {
            
            // Get all network interfaces addresses
            IPAddress[] localIPAddress = Dns.GetHostAddresses(Dns.GetHostName());

            // Change the current using address <Code>localIPAddress[0]</Code> to IPV4 string format
            foreach (IPAddress temIP in localIPAddress)
            {
                if (temIP.AddressFamily == AddressFamily.InterNetwork)
                {
                    LocalIP = temIP.ToString();
                    //break;
                    Console.WriteLine(temIP);
                }
            }
                       
        }
    }

Result:
192.168.79.1(vmWare)
192.168.23.1(vmWare)
192.168.0.102(mylocal)

How Do I know which one am I using now and how to choose it?
Thank You Guys!!

All replies (2)

Saturday, November 1, 2014 2:54 PM ✅Answered

Try code in following link

http://blog.stephencleary.com/2009/05/getting-local-ip-addresses.html

Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor


Saturday, November 1, 2014 3:59 PM ✅Answered

There's no such thing as a "primary" IP address. If you get three IP addresses using your code above, it means that the computer has three IP addresses assigned to it. You need to know which network interface you are using. If you have multiple network interfaces you have no idea which address goes with which network interface.

You could use the NetworkInterface class to find out if a particular network interface is up and whether it is active or not:

string ipAddress = "";
            var ni = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface item in ni)
            {
                if (item.OperationalStatus == OperationalStatus.Up) //&& item.NetworkInterfaceType == ?
                {
                    foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
                    {
                        if (ip.Address.AddressFamily == AddressFamily.InterNetwork & !IPAddress.IsLoopback(ip.Address))
                        {
                            ipAddress = ip.Address.ToString();
                            Console.WriteLine(ipAddress);
                        }
                    }
                }
            }

Please refer to the following link for more information about this: http://stackoverflow.com/questions/6803073/get-local-ip-address-c-sharp

And please remember to mark helpful posts as answer and/or helpful.