locked
Get IP Address of Local Area Connection for Ethernet Adapter

    Question

  • When I trying to get IP Address of my server. It is throwing with more than one address. I want to get only Local Area Connection IP Address.

    I have tried with IPConfig from CMD. Also, tried  DNS.GetHostEntry(DNS.GetHostName()).AddressList.

    Both are giving more than one IP Address. I want to get IP for Local Area Connection.

    Thanks in Advance for your help

    Monday, July 12, 2010 7:39 PM

Answers

  • Hi Kumarkumar10,

     

    Welcome to MSDN Forum!

     

    If you want to get the ip of the local net card, you can think about my steps following, and solve this issue.

     

    1)  Returns objects that describe the network interfaces on the local computer.

    2)  Then read the information of each NetworkInterface from the registry.

    3)  Make sure if this interface is a local net card.

    4)  Display the information.

     

     

     

    Following is a sample. This sample displays the all NetworkInterface information in the system.

     

    You only want the ip of the local net card, so you only need to make sure the card is a local net card and display the ip, and the other cards and information you need not to care about.

     

    Yellow and grey: It is the need of getting IP.

    Grey: It’s my different from the common method from the internet. I think this would help you more.

     

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Net;

    using System.Net.NetworkInformation;

    using System.Net.Sockets;

    using Microsoft.Win32;

     

    namespace NetCard

    { 

        class Program

        {

            static void Main(string[] args)

            {

                new Program().ShowNetworkInterfaceMessage();

                Console.ReadLine();

            }

     

            public void ShowNetworkInterfaceMessage()

            {

           // Returns objects that describe the network interfaces on the local computer.

                NetworkInterface[] fNetworkInterfaces =

                            NetworkInterface.GetAllNetworkInterfaces();

     

                // Then read the information of each NetworkInterface from the registry.

                foreach (NetworkInterface adapter in fNetworkInterfaces)

                {

                    string fCardType = "unknow net card";

     

                  // This registry key contains the information of the netcard interfaces

      // in your system.

                    // And we can open it to take a look at.

                    string fRegistryKey =

    "SYSTEM\\CurrentControlSet\\Control\\Network\\"

    +"{4D36E972-E325-11CE-BFC1-08002BE10318}\\"

                    + adapter.Id + "\\Connection";

     

                    // Retrieves a specified subkey,

      // and specifies the write access is not be applied to the key.

              RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);

     

                    // make sure if this interface is a local net card

                    if (rk != null)

                    {

                        // distinguish PnpInstanceID

                        // It could be a local net card if there is a PCI prefixion

     

                // Read the value of the specify registry key.

                  string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();

     

    // Because there is no one method to discriminate the net cards is correct absolutely.

     

    //Someone use the adapter.Description to discriminate,

    // but some local net cards's description are not contain the "PCI" key word.

     

    // I found it is not enough to only use the PnpInstanceID to discriminate,

    // because there're also some wireless net card are "PCI" type,

    // and the description also could contain the "PCI" key word.

     

    // But I have not found a method which use the DefaultNameResourceId

    // to help the program to discriminate the type.

    // So I analysis the regirstry and found that the DefaultNameResourceId can help us.

                string fDefaultNameResourceId

    = rk.GetValue("DefaultNameResourceId", "").ToString();

     

                  int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));

                        if (fPnpInstanceID.Length > 3 &&

                            fPnpInstanceID.Substring(0, 3) == "PCI")

                        {

              // Accorrding to the regirstry analytical to be sure

              // if this is a local net card or a wireless net card with PCI type.

                            if (fDefaultNameResourceId == "1803")

                                fCardType = "local net card";

                            else

                                fCardType = "wireless net card";

                        }

                        else if (fMediaSubType == 2)

                            fCardType = "wireless net card";

                        Console.WriteLine(fMediaSubType);

                    }

                    this.DisplayDescription(fCardType, adapter);

                }

            }

     

            // display the information

          private void DisplayDescription(string fCardType, NetworkInterface adapter)

            {

                Console.WriteLine("------------------------------------");

                Console.WriteLine("-- " + fCardType);

                Console.WriteLine("------------------------------------");

            // Gets the identifier of the network adapter. 

                Console.WriteLine("Id .................. : {0}", adapter.Id);

            // Gets the name of the network adapter.

                Console.WriteLine("Name ................ : {0}", adapter.Name);

            // Gets the description of the interface.

                Console.WriteLine("Description ......... : {0}", adapter.Description);

            // Gets the interface type.

                Console.WriteLine("Interface type ...... : {0}",

                                adapter.NetworkInterfaceType);

            // Gets a Boolean value that indicates

    // whether the network interface is set to only receive data packets.

             Console.WriteLine("Is receive only...... : {0}", adapter.IsReceiveOnly); 

            // Gets a Boolean value that indicates whether the network interface is

            //enabled to receive multicast packets. 

         Console.WriteLine("Multicast............ : {0}", adapter.SupportsMulticast);

            //Gets the speed of the network interface.

                Console.WriteLine("Speed ............... : {0}", adapter.Speed);

            // MAC Address 

                Console.WriteLine("Physical Address .... : {0}",

                        adapter.GetPhysicalAddress().ToString());

     

           IPInterfaceProperties fIPInterfaceProperties = adapter.GetIPProperties();

                UnicastIPAddressInformationCollection

    UnicastIPAddressInformationCollection

    = fIPInterfaceProperties.UnicastAddresses;

                foreach (UnicastIPAddressInformation UnicastIPAddressInformation in

                            UnicastIPAddressInformationCollection)

                {

     

                    if (UnicastIPAddressInformation.Address.AddressFamily

    == AddressFamily.InterNetwork)

                        Console.WriteLine("Ip Address .......... : {0}",

                    UnicastIPAddressInformation.Address); // Ip Address

                }

                Console.WriteLine();

            }

        }

    }

     

     

    If there’s anything unclear please feel free to let me know.

     

    Regards,

    Mike

    -------------------------------------------------------------------

    Please remember:

    Helpful à Mark as Answer

    This can be beneficial to other community members reading the thread.

    Thursday, July 15, 2010 3:54 AM
    Moderator

All replies

  • If you're getting more than 1 address then the server has more than 1 address... Depending on the OS you might get 2 (one IPv4 and one IPv6) even if only 1 is configured by you.

    Depending on the use you want to make of the IP(s) there might be easy ways to find out which is the relevant.

    Cristian.

     

     

    Monday, July 12, 2010 8:36 PM
  • Thanks for quick response.

    I have few VMware Network Adapters and few Virtual Box Networks set up in my server.

    I am looking for Ethernet LAN IP Address.  If there is any pointer to get Ethernet LAN IP, please let me know.

    Thanks

    Monday, July 12, 2010 10:19 PM
  • Hi Kumarkumar10,

     

    Welcome to MSDN Forum!

     

    If you want to get the ip of the local net card, you can think about my steps following, and solve this issue.

     

    1)  Returns objects that describe the network interfaces on the local computer.

    2)  Then read the information of each NetworkInterface from the registry.

    3)  Make sure if this interface is a local net card.

    4)  Display the information.

     

     

     

    Following is a sample. This sample displays the all NetworkInterface information in the system.

     

    You only want the ip of the local net card, so you only need to make sure the card is a local net card and display the ip, and the other cards and information you need not to care about.

     

    Yellow and grey: It is the need of getting IP.

    Grey: It’s my different from the common method from the internet. I think this would help you more.

     

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Net;

    using System.Net.NetworkInformation;

    using System.Net.Sockets;

    using Microsoft.Win32;

     

    namespace NetCard

    { 

        class Program

        {

            static void Main(string[] args)

            {

                new Program().ShowNetworkInterfaceMessage();

                Console.ReadLine();

            }

     

            public void ShowNetworkInterfaceMessage()

            {

           // Returns objects that describe the network interfaces on the local computer.

                NetworkInterface[] fNetworkInterfaces =

                            NetworkInterface.GetAllNetworkInterfaces();

     

                // Then read the information of each NetworkInterface from the registry.

                foreach (NetworkInterface adapter in fNetworkInterfaces)

                {

                    string fCardType = "unknow net card";

     

                  // This registry key contains the information of the netcard interfaces

      // in your system.

                    // And we can open it to take a look at.

                    string fRegistryKey =

    "SYSTEM\\CurrentControlSet\\Control\\Network\\"

    +"{4D36E972-E325-11CE-BFC1-08002BE10318}\\"

                    + adapter.Id + "\\Connection";

     

                    // Retrieves a specified subkey,

      // and specifies the write access is not be applied to the key.

              RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);

     

                    // make sure if this interface is a local net card

                    if (rk != null)

                    {

                        // distinguish PnpInstanceID

                        // It could be a local net card if there is a PCI prefixion

     

                // Read the value of the specify registry key.

                  string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();

     

    // Because there is no one method to discriminate the net cards is correct absolutely.

     

    //Someone use the adapter.Description to discriminate,

    // but some local net cards's description are not contain the "PCI" key word.

     

    // I found it is not enough to only use the PnpInstanceID to discriminate,

    // because there're also some wireless net card are "PCI" type,

    // and the description also could contain the "PCI" key word.

     

    // But I have not found a method which use the DefaultNameResourceId

    // to help the program to discriminate the type.

    // So I analysis the regirstry and found that the DefaultNameResourceId can help us.

                string fDefaultNameResourceId

    = rk.GetValue("DefaultNameResourceId", "").ToString();

     

                  int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));

                        if (fPnpInstanceID.Length > 3 &&

                            fPnpInstanceID.Substring(0, 3) == "PCI")

                        {

              // Accorrding to the regirstry analytical to be sure

              // if this is a local net card or a wireless net card with PCI type.

                            if (fDefaultNameResourceId == "1803")

                                fCardType = "local net card";

                            else

                                fCardType = "wireless net card";

                        }

                        else if (fMediaSubType == 2)

                            fCardType = "wireless net card";

                        Console.WriteLine(fMediaSubType);

                    }

                    this.DisplayDescription(fCardType, adapter);

                }

            }

     

            // display the information

          private void DisplayDescription(string fCardType, NetworkInterface adapter)

            {

                Console.WriteLine("------------------------------------");

                Console.WriteLine("-- " + fCardType);

                Console.WriteLine("------------------------------------");

            // Gets the identifier of the network adapter. 

                Console.WriteLine("Id .................. : {0}", adapter.Id);

            // Gets the name of the network adapter.

                Console.WriteLine("Name ................ : {0}", adapter.Name);

            // Gets the description of the interface.

                Console.WriteLine("Description ......... : {0}", adapter.Description);

            // Gets the interface type.

                Console.WriteLine("Interface type ...... : {0}",

                                adapter.NetworkInterfaceType);

            // Gets a Boolean value that indicates

    // whether the network interface is set to only receive data packets.

             Console.WriteLine("Is receive only...... : {0}", adapter.IsReceiveOnly); 

            // Gets a Boolean value that indicates whether the network interface is

            //enabled to receive multicast packets. 

         Console.WriteLine("Multicast............ : {0}", adapter.SupportsMulticast);

            //Gets the speed of the network interface.

                Console.WriteLine("Speed ............... : {0}", adapter.Speed);

            // MAC Address 

                Console.WriteLine("Physical Address .... : {0}",

                        adapter.GetPhysicalAddress().ToString());

     

           IPInterfaceProperties fIPInterfaceProperties = adapter.GetIPProperties();

                UnicastIPAddressInformationCollection

    UnicastIPAddressInformationCollection

    = fIPInterfaceProperties.UnicastAddresses;

                foreach (UnicastIPAddressInformation UnicastIPAddressInformation in

                            UnicastIPAddressInformationCollection)

                {

     

                    if (UnicastIPAddressInformation.Address.AddressFamily

    == AddressFamily.InterNetwork)

                        Console.WriteLine("Ip Address .......... : {0}",

                    UnicastIPAddressInformation.Address); // Ip Address

                }

                Console.WriteLine();

            }

        }

    }

     

     

    If there’s anything unclear please feel free to let me know.

     

    Regards,

    Mike

    -------------------------------------------------------------------

    Please remember:

    Helpful à Mark as Answer

    This can be beneficial to other community members reading the thread.

    Thursday, July 15, 2010 3:54 AM
    Moderator
  • Try NetworkInterface class defined in System.Net.NetworkInformation.

    You can get the network properties from that. Look out for Ethernet in Name property. Get the IP Address from GetIPProperties() method.

    The rough code is given below.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net.NetworkInformation;
    using System.Net;
    
    namespace IPAddressProgram
    {
      class Program
      {
        static void Main(string[] args)
        {
          NetworkInterface[] networks = NetworkInterface.GetAllNetworkInterfaces();
    
          foreach (NetworkInterface network in networks)
          {
    
            if (network.NetworkInterfaceType.ToString().Contains("Ethernet"))
            {
              ////Code for getting IP address
              Console.WriteLine("DNS Addresses");
              foreach (IPAddress entry in network.GetIPProperties().DnsAddresses)
              {
                Console.WriteLine(entry.ToString());
              }
    
              //Code for getting IP address
              Console.WriteLine("Anycast Addresses");
              foreach (IPAddressInformation entry in network.GetIPProperties().AnycastAddresses)
              {
                Console.WriteLine(entry.ToString());
              }
    
              //Code for getting IP address
              Console.WriteLine("DHCPServer Addresses");
              foreach (IPAddress entry in network.GetIPProperties().DhcpServerAddresses)
              {
                Console.WriteLine(entry.ToString());
              }
    
              //Code for getting IP address
              Console.WriteLine("Gateway Addresses");
              foreach (GatewayIPAddressInformation entry in network.GetIPProperties().GatewayAddresses)
              {
                Console.WriteLine(entry.Address.ToString());
              }
    
              //Code for getting IP address
              Console.WriteLine("Multicast Addresses");
              foreach (MulticastIPAddressInformation entry in network.GetIPProperties().MulticastAddresses)
              {
                Console.WriteLine(entry.Address.ToString());
              }
    
              //Code for getting IP address
              Console.WriteLine("Unicast Addresses");
              foreach (UnicastIPAddressInformation entry in network.GetIPProperties().UnicastAddresses)
              {
                Console.WriteLine(entry.Address.ToString());
              }
            }
          }
    
          Console.ReadKey(true);
        }
      }
    }
    The last one i.e Unicast address may do the trick for you.
    Regards, Chethan Kamath. "Remember that if the world didn't suck, we'd all fall off."
    Thursday, July 15, 2010 7:59 AM
  • Hi Kumarkumar10,

     

    I would like to know if the issue had been solved.

     

    If you still have any doubt and concern about this issue, please let me know.

     

    If you think which reply is help for you, please mark it, this can be beneficial to other community members reading the thread.

     

    Have a nice day!

    Mike

     

    Monday, July 19, 2010 7:59 AM
    Moderator
  • This is a bit useful. I am able to get the LAN IP .

     

    Thanks,

    Prabhu.

    Thursday, July 22, 2010 10:45 PM