Retrieving gateway addresses from System.Net.NetworkInformation
-
Monday, August 13, 2007 9:56 PM
Hi,
After my previous thread about retrieving the ip details of the local machine, Jo0ls told me to look at the system.net.networkinformation namespace to do this. However, im still unable to retrieve my gateway adress in vb 2008 express.
I used my own code to try and display my local gateway address which produced an output of:
System.Net.NetworkInformation.SystemGatewayIPAddressInformation
I couldnt find the problem so I used MSDNs code of:
Console.WriteLine(
"Gateways") Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces() Dim adapter As NetworkInterface For Each adapter In adapters Dim adapterProperties As IPInterfaceProperties = adapter.GetIPProperties() Dim addresses As GatewayIPAddressInformationCollection = adapterProperties.GatewayAddresses If addresses.Count > 0 ThenConsole.WriteLine(adapter.Description)
Dim address As GatewayIPAddressInformation For Each address In addressesConsole.WriteLine(
" Gateway Address ......................... : {0}", address.ToString()) Next addressConsole.WriteLine()
End If Next adapterthis produced an ouput of:
Gateways
WAN (PPP/SLIP) Interface
Gateway Address ......................... : System.Net.NetworkInformation.SystemGatewayIPAddressInformation
Is this the right coe for displaying your IP gateway? Can somebody please tell me whats going on here?
Thanks
Paulio
All Replies
-
Thursday, August 16, 2007 5:10 AMModerator
Hi Paulio,
Based on your post, I know that the upper code doesn't get the GateWay information. Please try the following code snippet to get GateWay information. If you have any further questions about VB 2008 Express. Please post your issues on Visual Basic Express Orcas forum. This forum is specially for handling Visual Basic Express Orcas issues. You can get the better assistance to your issues. Hope this helps.
Code SnippetImports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Net
Imports System.Net.NetworkInformation
Module Module1
Sub Main()
ShowNetworkInformation()
Console.ReadLine()
End Sub
Public Sub ShowNetworkInformation()
Dim ipProperties As IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties()
Console.WriteLine("Host name: {0}", ipProperties.HostName)
Console.WriteLine("Domain name: {0}", ipProperties.DomainName)
For Each networkCard As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()
Console.WriteLine("Interface: {0}", networkCard.Id)
Console.WriteLine("" & Chr(9) & " Name: {0}", networkCard.Name)
Console.WriteLine("" & Chr(9) & " Description: {0}", networkCard.Description)
Console.WriteLine("" & Chr(9) & " Status: {0}", networkCard.OperationalStatus)
Console.WriteLine("" & Chr(9) & " MAC Address: {0}", networkCard.GetPhysicalAddress().ToString())
Console.WriteLine("" & Chr(9) & " Gateway Address:")
For Each gatewayAddr As GatewayIPAddressInformation In networkCard.GetIPProperties().GatewayAddresses
Console.WriteLine("" & Chr(9) & "" & Chr(9) & " Gateway entry: {0}", gatewayAddr.Address.ToString())
Next
Console.WriteLine("" & Chr(9) & " DNS Settings:")
For Each address As IPAddress In networkCard.GetIPProperties().DnsAddresses
Console.WriteLine("" & Chr(9) & "" & Chr(9) & " DNS entry: {0}", address.ToString())
Next
Next
End Sub
End Module
Thanks for your questions.

