none
How to get Either net card IP address of my pc RRS feed

  • Question

  • when i am testing below code which is showing my VMWare IP address but how to get my either net card IP address.

     public static string GetIPAddress()
            {
    
                var host = Dns.GetHostEntry(Dns.GetHostName());
                foreach (var ip in host.AddressList)
                {
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                    {
                        return ip.ToString();
                    }
                }
    
                throw new Exception("No network adapters with an IPv4 address in the system!");
            }
    how to fetch either net card or my network lan card IP address by c#. thanks

    Monday, July 22, 2019 9:50 AM

Answers

  • You can use WMI to enumerate all your network adapters (there could be several) and then for each adapter enumerate all the IP addresses (there could also be several).

    The following thread contains some sample code that you can copy:

    https://social.msdn.microsoft.com/Forums/en-US/4328bbaf-5e98-4277-914b-af127b9bf931/wmi-query-to-get-ip-addressdomain-nameosversion

    • Marked as answer by Sudip_inn Tuesday, July 23, 2019 10:44 PM
    Monday, July 22, 2019 9:59 AM
    Moderator
  • You want to look for Ethernet, not either net. The word ether can be loosely defined (in this context) as meaning air.



    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by Sudip_inn Tuesday, July 23, 2019 10:44 PM
    Tuesday, July 23, 2019 2:56 AM
  • Hi Sudip_inn, 

    Thank you for posting here.

    For your question, you want to get Ethernet card IP address.

    I have made a test and you can refer the following code:

            public static string GetLocalIpAddress()
            {
                UnicastIPAddressInformation mostSuitableIp = null;
    
                var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
    
                foreach (var network in networkInterfaces)
                {
                    if (network.OperationalStatus != OperationalStatus.Up)
                        continue;
    
                    var properties = network.GetIPProperties();
    
                    if (properties.GatewayAddresses.Count == 0)
                        continue;
    
                    foreach (var address in properties.UnicastAddresses)
                    {
                        if (address.Address.AddressFamily != AddressFamily.InterNetwork)
                            continue;
    
                        if (IPAddress.IsLoopback(address.Address))
                            continue;
    
                        if (!address.IsDnsEligible)
                        {
                            if (mostSuitableIp == null)
                                mostSuitableIp = address;
                            continue;
                        }
    
                        if (address.PrefixOrigin != PrefixOrigin.Dhcp)
                        {
                            if (mostSuitableIp == null || !mostSuitableIp.IsDnsEligible)
                                mostSuitableIp = address;
                            continue;
                        }
    
                        return address.Address.ToString();
                    }
                }
    
                return mostSuitableIp != null
                    ? mostSuitableIp.Address.ToString()
                    : "";
            }

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    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 Sudip_inn Tuesday, July 23, 2019 10:44 PM
    Tuesday, July 23, 2019 6:35 AM
    Moderator
  • Are you doing this inside the VMWare virtual machine?  A VM cannot get the IP address of the VM host, and there's no reason why it should need it.  Running a VM is, philosophically, exactly like running a separate machine.  If you need to get the IP address of the host in order to send packets, then you will have to search for its host name, exactly as if you were sending it from another physical computer.

    If you are trying to create a listening socket, then you DO want the VM's IP address, not the host.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by Sudip_inn Tuesday, July 23, 2019 10:44 PM
    Tuesday, July 23, 2019 10:11 PM

All replies

  • You can use WMI to enumerate all your network adapters (there could be several) and then for each adapter enumerate all the IP addresses (there could also be several).

    The following thread contains some sample code that you can copy:

    https://social.msdn.microsoft.com/Forums/en-US/4328bbaf-5e98-4277-914b-af127b9bf931/wmi-query-to-get-ip-addressdomain-nameosversion

    • Marked as answer by Sudip_inn Tuesday, July 23, 2019 10:44 PM
    Monday, July 22, 2019 9:59 AM
    Moderator
  • You want to look for Ethernet, not either net. The word ether can be loosely defined (in this context) as meaning air.



    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by Sudip_inn Tuesday, July 23, 2019 10:44 PM
    Tuesday, July 23, 2019 2:56 AM
  • Hi Sudip_inn, 

    Thank you for posting here.

    For your question, you want to get Ethernet card IP address.

    I have made a test and you can refer the following code:

            public static string GetLocalIpAddress()
            {
                UnicastIPAddressInformation mostSuitableIp = null;
    
                var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
    
                foreach (var network in networkInterfaces)
                {
                    if (network.OperationalStatus != OperationalStatus.Up)
                        continue;
    
                    var properties = network.GetIPProperties();
    
                    if (properties.GatewayAddresses.Count == 0)
                        continue;
    
                    foreach (var address in properties.UnicastAddresses)
                    {
                        if (address.Address.AddressFamily != AddressFamily.InterNetwork)
                            continue;
    
                        if (IPAddress.IsLoopback(address.Address))
                            continue;
    
                        if (!address.IsDnsEligible)
                        {
                            if (mostSuitableIp == null)
                                mostSuitableIp = address;
                            continue;
                        }
    
                        if (address.PrefixOrigin != PrefixOrigin.Dhcp)
                        {
                            if (mostSuitableIp == null || !mostSuitableIp.IsDnsEligible)
                                mostSuitableIp = address;
                            continue;
                        }
    
                        return address.Address.ToString();
                    }
                }
    
                return mostSuitableIp != null
                    ? mostSuitableIp.Address.ToString()
                    : "";
            }

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    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 Sudip_inn Tuesday, July 23, 2019 10:44 PM
    Tuesday, July 23, 2019 6:35 AM
    Moderator
  • Are you doing this inside the VMWare virtual machine?  A VM cannot get the IP address of the VM host, and there's no reason why it should need it.  Running a VM is, philosophically, exactly like running a separate machine.  If you need to get the IP address of the host in order to send packets, then you will have to search for its host name, exactly as if you were sending it from another physical computer.

    If you are trying to create a listening socket, then you DO want the VM's IP address, not the host.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by Sudip_inn Tuesday, July 23, 2019 10:44 PM
    Tuesday, July 23, 2019 10:11 PM
  • Sir your code work. thanks a lot but many area in code not clear

    1) NetworkInterface.GetAllNetworkInterfaces();

    does the above line will give me all lan card info ?

    2) what is the meaning of this line

    if (network.OperationalStatus != OperationalStatus.Up)
                        continue;

    3) what is the meaning of this line

    var properties = network.GetIPProperties();

                    if (properties.GatewayAddresses.Count == 0)
                        continue;

    4) what is loopback means here ?

    if (IPAddress.IsLoopback(address.Address))
                            continue;

    5) what is meaning of these line

    if (!address.IsDnsEligible)
                        {
                            if (mostSuitableIp == null)
                                mostSuitableIp = address;
                            continue;
                        }

    6) what is meaning of this line

    if (address.PrefixOrigin != PrefixOrigin.Dhcp)
                        {
                            if (mostSuitableIp == null || !mostSuitableIp.IsDnsEligible)
                                mostSuitableIp = address;
                            continue;
                        }

    sorry to ask you so many question. i will be grateful if you answer point wise as i asked. thanks

    Wednesday, July 24, 2019 8:27 PM
  • You could have received most of these answers more quickly by using Google.  Really, you need to be able to investigate for yourself.

    1) Yes, but if you are inside a VM, that only gives you the network interfaces that the VM sees.

    2) It should be obvious.  It skips any network interfaces that are down.

    3) It should be obvious.  It skips any network interfaces that do not have a gateway assigned.  A network interface cannot send to the outside world without a gateway configured.

    4) Really?  "Loopback" is the name of the special 127.0.0.1 address that always refers to the local computer, without going out to any network.

    5) There are some special IP address ranges that cannot be assigned to network interfaces, like the broadcast ranges.  This checks for and skips such addresses.

    6) This is saying that an address assigned by DHCP is probably the most useful address for this computer.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Wednesday, July 24, 2019 9:04 PM