I'm trying to figure out how to view the default gatways ip address and then get the mac address from that
Any ideas on how to see what the computers defualt gateway is?
Printable View
I'm trying to figure out how to view the default gatways ip address and then get the mac address from that
Any ideas on how to see what the computers defualt gateway is?
The gateway is defined on a per network card level, so this code will get you the Gateway for each of the NICs installed on a machine:
Code:Dim sb As New StringBuilder
sb.AppendLine()
For Each n As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()
sb.AppendLine(n.Description)
sb.AppendLine("MAC:" & n.GetPhysicalAddress().ToString())
For Each u As UnicastIPAddressInformation In n.GetIPProperties().UnicastAddresses
sb.AppendLine("IP:" & u.Address.ToString())
Next
For Each g As GatewayIPAddressInformation In n.GetIPProperties().GatewayAddresses
sb.AppendLine("Gateway:" & g.Address.ToString())
Next
Next
MessageBox.Show(sb.ToString())
Well the default gateway varies for every user.
For example, my default gateway is 192.168.1.1 (that's the router default gateway).
Take a look at this example to get the default gateway:
http://www.java2s.com/Tutorial/CShar...figuration.htm
It's in C# but mentioned and VB.Net are very similar so there shouldn't be any problems porting the code.
You might also want to take a look at this VB.Net example, just to get some ideas (since this code requires user defined information)
http://www.xtremevbtalk.com/archive/.../t-251709.html
Also this VB6 example:
http://bytes.com/topic/visual-basic-...efault-gateway
Happy coding :)
*EDIT* Negative0: damn, you won this time... it took too long time for me to paste the links xD
I am working in c# so i was going to converet it anyway. There are alot more people in the vb rooms so its usually faster with more information if i just post here and then convert it over
StringBuilder sb = new StringBuilder();
sb.AppendLine();
foreach (NetworkInterface n in NetworkInterface.GetAllNetworkInterfaces())
{
sb.AppendLine(n.Description);
sb.AppendLine("MAC:" + n.GetPhysicalAddress().ToString());
foreach (UnicastIPAddressInformation u in n.GetIPProperties().UnicastAddresses)
{
sb.AppendLine("IP:" + u.Address.ToString());
}
foreach (GatewayIPAddressInformation g in n.GetIPProperties().GatewayAddresses)
{
sb.AppendLine("Gateway:" + g.Address.ToString());
}
}
MessageBox.Show(sb.ToString());