none
Get the IP address of the physical network card ? RRS feed

  • Question

  • On the computer, there is one physical network card but there are many logical network cards (logical network card = virtual network card), when running code C# computer to get the IP of the logical network card, what are the ways you can identify the physical network card and virtual network card and get physical network IP card ?
    Tuesday, March 17, 2020 9:23 AM

Answers

  • Hi lamtriendong,
    You can use NetworkInterface.GetAllNetworkInterfaces method to get the objects that describe the network interfaces on the local computer.
    And as Castorix31 said, filter with physical NICs that should be listed in the registry. If there is PCI in front, it is the real network card of this machine.
    Here is a code example you can refer to.

    public static IList<string> GetPhysicsNetworkCardIP()
    {
        var networkCardIPs = new List<string>();
        NetworkInterface[] fNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface adapter in fNetworkInterfaces)
        {
            string fRegistryKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + adapter.Id + "\\Connection";
            RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
            if (rk != null)
            {
                // If there is PCI in front, it is the real network card of this machine.
                string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
                int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
                if (fPnpInstanceID.Length > 3 && fPnpInstanceID.Substring(0, 3) == "PCI")
                {
                    IPInterfaceProperties fIPInterfaceProperties = adapter.GetIPProperties();
                    UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = fIPInterfaceProperties.UnicastAddresses;
                    foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)
                    {
                        if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            networkCardIPs.Add(UnicastIPAddressInformation.Address.ToString());
                        }
    
                    }
                }
                        
            }
        }
    
        return networkCardIPs;
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by lamtriendong Wednesday, March 25, 2020 8:52 AM
    Wednesday, March 18, 2020 3:22 AM

All replies

  • You can get the IP addresses with IPInterfaceProperties.UnicastAddresses

    and filter with physical network cards which should be listed in registry at :

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards

    Tuesday, March 17, 2020 11:28 AM
  • Hi lamtriendong,
    You can use NetworkInterface.GetAllNetworkInterfaces method to get the objects that describe the network interfaces on the local computer.
    And as Castorix31 said, filter with physical NICs that should be listed in the registry. If there is PCI in front, it is the real network card of this machine.
    Here is a code example you can refer to.

    public static IList<string> GetPhysicsNetworkCardIP()
    {
        var networkCardIPs = new List<string>();
        NetworkInterface[] fNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface adapter in fNetworkInterfaces)
        {
            string fRegistryKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + adapter.Id + "\\Connection";
            RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
            if (rk != null)
            {
                // If there is PCI in front, it is the real network card of this machine.
                string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
                int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
                if (fPnpInstanceID.Length > 3 && fPnpInstanceID.Substring(0, 3) == "PCI")
                {
                    IPInterfaceProperties fIPInterfaceProperties = adapter.GetIPProperties();
                    UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = fIPInterfaceProperties.UnicastAddresses;
                    foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)
                    {
                        if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            networkCardIPs.Add(UnicastIPAddressInformation.Address.ToString());
                        }
    
                    }
                }
                        
            }
        }
    
        return networkCardIPs;
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by lamtriendong Wednesday, March 25, 2020 8:52 AM
    Wednesday, March 18, 2020 3:22 AM
  • Hi lamtriendong,
    Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly. If not solved, what problem did you encounter?
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 23, 2020 9:47 AM
  • Thank you for only helping me writen
    Wednesday, March 25, 2020 8:53 AM