0

How can one get the default IP address excluding the 127.0.0.1 loopback address when mutliple IP addresses are assigned to PC i.e if the PC is multihomed.

Following code returns correct default IP address on one PC but returns incorrect IP address on another PC, so there must be some other solution.

    private string[] GetDefaultIPWithSubnet()
    {
        ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection moc = mc.GetInstances();
        string[] ipSubnet = new string[2];
        foreach (ManagementObject mo in moc)
        {
            if ((bool)mo["IPEnabled"])
            {
                try
                {
                    string[] ips = (string[])mo["IPAddress"];
                    string[] subnets = (string[])mo["IPSubnet"];
                    ipSubnet[0] = ips[0].ToString();
                    ipSubnet[1] = subnets[0].ToString();
                    break;
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        return ipSubnet;
    }
svick
  • 236,525
  • 50
  • 385
  • 514
Software Engineer
  • 3,906
  • 1
  • 26
  • 35
  • 1
    What's the definition of default ip address of the machine? I would say 127.0.0.1, but you might want something else... PS: You might want to look at this: http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.aspx – Michal B. May 08 '12 at 07:09
  • Here I meant the one we use for LAN/internet excluding the 127.0.0.1 loopback address. – Software Engineer May 08 '12 at 07:19
  • 1
    And what if you have two network interfaces, which is very common on server machines. Which one is the default one then? I am trying to point you in a direction, so you understand there is no such thing. You should get all ip addresses and figure out which ones you are interested in. – Michal B. May 08 '12 at 07:22
  • 4
    it kinda also depends on routing configuration, i.e. *where you are trying to get to* - but in simple terms: there is no such thing as a default – Marc Gravell May 08 '12 at 07:25
  • 2
    And also be aware that you can have multiple ip addresses assigned to one network interface... – Michal B. May 08 '12 at 07:34
  • Is there any workaround to get the default ip address if we have single network interface ? – Software Engineer May 08 '12 at 07:37

3 Answers3

4
public static void GetDefaultIp()
{
    NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
    foreach (NetworkInterface adapter in adapters)
    {
        if (adapter.OperationalStatus == OperationalStatus.Up && adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet) 
        {
            IPInterfaceProperties properties = adapter.GetIPProperties();
            foreach (var x in properties.UnicastAddresses)
            {
                if (x.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    Console.WriteLine(" IPAddress ........ : {0:x}", x.Address.ToString());
            }
        }
    }
}
Romil Kumar Jain
  • 20,239
  • 9
  • 63
  • 92
  • This is a good one assuming you have one network card with single ip address assigned. Looks clean too. I will upvote it ;) – Michal B. May 08 '12 at 10:51
3

I think you mean the interface with the default route. You can get the IPv4 route table with the GetIpForwardTable function (quick google reveals that it is callable through p/invoke) and look for a 0.0.0.0 destination route (run route print at command line to check what a route table looks like).

cynic
  • 5,305
  • 1
  • 24
  • 40
0

I think you misunderstood the meaning of IPEnabled, as far as I know that parameter is TRUE if TCP/IP is enabled on the interface. So I don't think this is what you're looking for.

raz3r
  • 3,071
  • 8
  • 44
  • 66