1

When using IIS 7 Manager to create a web site, you can set the new site's ip address by selecting from a drop-down list. Is there a way to programmatically obtain this list?

I do not see it in applicationHost.config, so I'm not sure where to look.

Thanks.

Liam
  • 27,717
  • 28
  • 128
  • 190
poolboy
  • 53
  • 5
  • Check out this answer, quite relevant : http://stackoverflow.com/questions/5271724/get-all-ip-addresses-on-machine – Kzest Jun 18 '13 at 16:25

1 Answers1

0

Based on the link provided by Kzest, it looks like there are two ways of accomplishing this. In the examples below, I am screening on AddressFamily to only show items that look like 'xxx.yyy.zz.q'.

Also, the second method also returns the 127.0.0.1 address, which, for my purposes makes it less useful.

private void showIpAddresses()
{
  //using System.Net
  IPHostEntry hostEntry=Dns.GetHostEntry(Dns.GetHostName());
  foreach(IPAddress ipAddress in hostEntry.AddressList)
    if(ipAddress.AddressFamily.Equals(System.Net.Sockets.AddressFamily.InterNetwork))
      Console.WriteLine(ipAddress.ToString());
}

private void showIpAddresses2()
{
  //using System.Net.NetworkInformation
  foreach(NetworkInterface nwi in NetworkInterface.GetAllNetworkInterfaces())
  {
    IPInterfaceProperties ipProperties=nwi.GetIPProperties();
    foreach(UnicastIPAddressInformation ipAddress in ipProperties.UnicastAddresses)
      if(ipAddress.Address.AddressFamily.Equals(System.Net.Sockets.AddressFamily.InterNetwork))
        Console.WriteLine(ipAddress.Address.ToString());
  }
}

Either of these meets my needs and answers the question. Thanks to Kzest for pointing me in the right direction.

poolboy
  • 53
  • 5