How do i use System.Net.NetworkInformation.GatewayIPAddressInformation Class?

10,914

From MSDN:

  • In brief, you call NetworkInterface.GetAllNetworkInterfaces() to get a set of adapters.
  • On one of those adapters, get adapter.GetIPProperties().GatewayAddresses.
  • Each element of the GatewayAddresses is a GatewayIPAddressInformation.

public static void DisplayGatewayAddresses()
{
    Console.WriteLine("Gateways");
    NetworkInterface[] adapters  = NetworkInterface.GetAllNetworkInterfaces();
    foreach (NetworkInterface adapter in adapters)
    {
        IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
        GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses;
        if (addresses.Count >0)
        {
            Console.WriteLine(adapter.Description);
            foreach (GatewayIPAddressInformation address in addresses)
            {
                Console.WriteLine("  Gateway Address ......................... : {0}", 
                    address.Address.ToString());
            }
            Console.WriteLine();
        }
    }
}
Share:
10,914
Erx_VB.NExT.Coder
Author by

Erx_VB.NExT.Coder

I'm passionate about code, since there's nothing else that lets you to put your thoughts, ideas & mental routines - into a computer, to do things your brain could only dream about and imagine, but never perform. Parts of my brain now live on... in various computers around the world, and if you're reading this, so does yours! :) Now that's something to think about; (pun unintended).

Updated on June 06, 2022

Comments

  • Erx_VB.NExT.Coder
    Erx_VB.NExT.Coder almost 2 years

    I tried every trick in the book, creating a new object, instantiating it (even tho it says i can't) and just trying to create a reference to use it, and then also trying to call the .Address value inside it while using it like i would use a shared member (without instantiation) and nothing is working, msdn sample/help is useless... i even tried inheriting it and useing it like that, none of them worked, i am sure i am missing something, can anyone give me some code samples? here is the msdn docs on it to to give you an overview...

    http://msdn.microsoft.com/en-us/library/system.net.networkinformation.gatewayipaddressinformation.aspx

    although i like vb.net more i can read and coded in both c# and vb.net so either will be fine.

    thanks:)

    an aside: if anyone is wondering why, im just trying to get my routers name/label from a pc as shown here... How to get Router Name & IP as shown in Windows Network Tab? (in Code)

  • Erx_VB.NExT.Coder
    Erx_VB.NExT.Coder over 13 years
    ahh, thanks, i have been looping through this actually without realizing the type (thanks to implicit declaration lol) looking for the elusive 'router name' that i've been looking for the last 3 days now... thank you...
  • Erx_VB.NExT.Coder
    Erx_VB.NExT.Coder over 13 years
    ps: i am looking on how to get router name of basic/user SOHO router as here, but can't find it in networkInterface class... stackoverflow.com/questions/3839189/…