[SOLVED] How to find the IP Address I'm using (Ethernet vs. Wi-Fi)?

Stout 286 Reputation points
2021-09-30T17:16:28.707+00:00

Hi. I need to find the IPv4 address of my PC. When I run IPConfig I get two IP Addresses, one for the "Ethernet adapter vEthernet (Default Switch)" and one for the "Wireless LAN adapter Wi-Fi."

My laptop has no ethernet switch, and is currently connected to the Wi-Fi network. The following API returns both ethernet and wi-fi IP addresses. How can I determine which one is what I am truly connected to?

Thanks.

IPAddress[] ipv4Addresses = Array.FindAll(Dns.GetHostEntry(string.Empty).AddressList,
                                               a => a.AddressFamily == AddressFamily.InterNetwork);
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,038 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 86,036 Reputation points
    2021-09-30T22:13:49.95+00:00

    You can use Network List Manager

    // Add reference to : Network List Manager 1.0 Type Library
    // Add : using NETWORKLIST;

    A test :

    NetworkListManager nlm = new NetworkListManager();
    IEnumNetworks networks = nlm.GetNetworks(NLM_ENUM_NETWORK.NLM_ENUM_NETWORK_CONNECTED);
    foreach (INetwork netw in networks)
    {  
        Console.WriteLine("Connected Network : " + netw.GetName() + " - Internet : " + netw.IsConnectedToInternet.ToString());
        foreach (INetworkConnection nc in netw.GetNetworkConnections())
        {                   
            string sIP = GetIPFromAdapter("{" + nc.GetAdapterId().ToString() + "}");
            if (sIP != null)
            {
                Console.WriteLine(sIP);
            }
        }
    }
    

    Utility function (remove static if not needed) :

    static public string GetIPFromAdapter(string sAdapterID)
    {
        string sRet = null;
        foreach (System.Net.NetworkInformation.NetworkInterface ni in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
        {
            if (ni.Id.Equals(sAdapterID, StringComparison.CurrentCultureIgnoreCase))                    
            {
                foreach (System.Net.NetworkInformation.UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
                {
                    if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    {
                        sRet = ip.Address.ToString();
                        break;
                    }
                }
            }
        }
        return sRet;
    }
    
    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. Michael Taylor 55,051 Reputation points
    2021-09-30T18:15:25.337+00:00

    I think you're asking the wrong question. You do have 2 IP addresses. One is your wifi adapter. The other is coming from the virtual network and that tells me you probably have Hyper-V turned on or VPN software installed. Both of these route your physical ethernet through a virtual switch. Which one you use (which could be both by the way) is dependent upon a variety of things but ultimately is controlled by the bindings that Windows is using. For example if you are connecting to a VM that you are hosting locally it is likely going through the virtual ethernet that is configured and the IP address is coming from either Hyper-V or perhaps passthrough to your actual DHCP server, depending upon how it is configured. But if you're using a browser then it is likely running through your wifi adapter.

    So "which one am I truly connected to" depends. What do you intend to do with this information? If you want to use it to connect to an Internet resource then the better question might be "which one has internet access". Unfortunately that is difficult to test outside of just pinging. This SO post has some suggestions but none of them are reliable or full proof.

    1 person found this answer helpful.

  2. Charles Thivierge 4,061 Reputation points
    2021-09-30T18:33:43.24+00:00

    you could run this PS command

    Get-NetIPAddress | ft IPAddress,InterfaceAlias

    It will list you all the IP address and the Interface Alias...

    something like this.. (You may have more entry actually)

    IPAddress InterfaceAlias

    169.254.99.68 Ethernet
    192.168.1.223 Wi-Fi
    127.0.0.1 Loopback Pseudo-Interface 1

    The look for "Wi-Fi" or something like that...

    Ethernet is the physical network adapter.

    Normally, a valid IP address for a network should never start with 169.254.x.x

    hth

    1 person found this answer helpful.

  3. BurrWalnut 11 Reputation points
    2021-09-30T17:26:31.067+00:00

    INTERNAL IP ADDRESS
    Open a Run window (Windows Logo key+R), type cmd /k ipconfig (note the two spaces), press Enter and the internal IP Address of the computer (usually beginning with 192.168) will be displayed.

    EXTERNAL IP ADDRESS
    Your external IP Address can be found here http://whatismyipaddress.com/


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.