Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 651
Erx_VB.NExT.Coder Avatar asked Jan 22 '23 08:01

Erx_VB.NExT.Coder


1 Answers

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();
        }
    }
}
like image 56
abelenky Avatar answered May 14 '23 01:05

abelenky