Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Get IP from IPInterfaceProperties

Get IP from IPInterfaceProperties

Scheduled Pinned Locked Moved C#
questioncom
3 Posts 2 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Johan Martensson
    wrote on last edited by
    #1

    Hi, I'm trying to get the IP, subnet and gateway for my interfaces using IPInterfaceProperties but both UnicastAddresses and GatewayAddresses returns a collection... How do I know which one to use? Here is my loop and for now I'm just using UnicastAddresses[0] and GatewayAddresses[0] but that isn't always right

    internal static NetworkAdapter[] AddExtraInformation(ArrayList adapters)
    {
    NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
    foreach (NetworkAdapter adapter in adapters)
    {
    foreach (NetworkInterface networkInterface in networkInterfaces)
    {
    if (!DescriptionToName(networkInterface.Description).Equals(adapter.DeviceName))
    continue;

    		adapter.Name = networkInterface.Name;
    		adapter.OperationalStatus = networkInterface.OperationalStatus.ToString();
    			
    		IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
    
    		if (ipProperties.UnicastAddresses != null)
    			if (ipProperties.UnicastAddresses.Count > 0)
    			{
    				adapter.Address = ipProperties.UnicastAddresses\[0\].Address.ToString();
    				adapter.SubnetMask = ipProperties.UnicastAddresses\[0\].IPv4Mask.ToString();
    			}
    			
    		if (ipProperties.GatewayAddresses != null)
    			if (ipProperties.GatewayAddresses.Count > 0) 
    				adapter.Gateway = ipProperties.GatewayAddresses\[0\].Address.ToString();
    		
    		break;
    	}
    }
    
    return (NetworkAdapter\[\])adapters.ToArray(typeof(NetworkAdapter));
    

    }

    http://johanmartensson.se - Home of MPEG4Watcher http://www.tinywebradio.com - Home of TinyWebRadio

    D 1 Reply Last reply
    0
    • J Johan Martensson

      Hi, I'm trying to get the IP, subnet and gateway for my interfaces using IPInterfaceProperties but both UnicastAddresses and GatewayAddresses returns a collection... How do I know which one to use? Here is my loop and for now I'm just using UnicastAddresses[0] and GatewayAddresses[0] but that isn't always right

      internal static NetworkAdapter[] AddExtraInformation(ArrayList adapters)
      {
      NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
      foreach (NetworkAdapter adapter in adapters)
      {
      foreach (NetworkInterface networkInterface in networkInterfaces)
      {
      if (!DescriptionToName(networkInterface.Description).Equals(adapter.DeviceName))
      continue;

      		adapter.Name = networkInterface.Name;
      		adapter.OperationalStatus = networkInterface.OperationalStatus.ToString();
      			
      		IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
      
      		if (ipProperties.UnicastAddresses != null)
      			if (ipProperties.UnicastAddresses.Count > 0)
      			{
      				adapter.Address = ipProperties.UnicastAddresses\[0\].Address.ToString();
      				adapter.SubnetMask = ipProperties.UnicastAddresses\[0\].IPv4Mask.ToString();
      			}
      			
      		if (ipProperties.GatewayAddresses != null)
      			if (ipProperties.GatewayAddresses.Count > 0) 
      				adapter.Gateway = ipProperties.GatewayAddresses\[0\].Address.ToString();
      		
      		break;
      	}
      }
      
      return (NetworkAdapter\[\])adapters.ToArray(typeof(NetworkAdapter));
      

      }

      http://johanmartensson.se - Home of MPEG4Watcher http://www.tinywebradio.com - Home of TinyWebRadio

      D Offline
      D Offline
      dataminers
      wrote on last edited by
      #2

      string hostName = Dns.GetHostName(); IPHostEntry ipEntry = Dns.GetHostEntry(hostName); foreach (IPAddress ip in ipEntry.AddressList) { //Please check ip values } For example, if you want to get IPv4 value, use "ip" variable which is using foreach statement; if (ip.AddressFamily.Equals(System.Net.Sockets.AddressFamily.InterNetwork)) string result = ip.ToString(); Best Regard...

      J 1 Reply Last reply
      0
      • D dataminers

        string hostName = Dns.GetHostName(); IPHostEntry ipEntry = Dns.GetHostEntry(hostName); foreach (IPAddress ip in ipEntry.AddressList) { //Please check ip values } For example, if you want to get IPv4 value, use "ip" variable which is using foreach statement; if (ip.AddressFamily.Equals(System.Net.Sockets.AddressFamily.InterNetwork)) string result = ip.ToString(); Best Regard...

        J Offline
        J Offline
        Johan Martensson
        wrote on last edited by
        #3

        Thanks for your reply I'm not sure how to tell which nic has the ip-number that I get from using your code. Anyway, I changed my code to this, looping the addresses and checking for IsDnsEligible, that seems to do the trick or am I missing something?

        foreach (UnicastIPAddressInformation address in ipProperties.UnicastAddresses)
        {
        if(address.IsDnsEligible)
        {
        adapter.Address = address.Address.ToString();
        adapter.SubnetMask = address.IPv4Mask.ToString();
        break;
        }
        }

        http://johanmartensson.se - Home of MPEG4Watcher http://www.tinywebradio.com - Home of TinyWebRadio

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups