vavtech
vavtech

Reputation: 41

C# How get default gateways (routes) and their metrics from disabled or disconnected network adapters (interfaces)?

How get default gateways (routes) and their metrics from disabled or disconnected network adapters (interfaces) ?

same as result using "netsh int ip show address" or "route print" or "netsh interface ip show route", its persistence routes:

also

list gateways

  1. WMI Class Win32_NetworkAdapterConfiguration works only for enabled and connected adapters:

string[] gateways = (string[]) objMO["DefaultIPGateway"]; UInt16[] gatewaysMetrics = (UInt16[]) objMO["GatewayCostMetric"];

  1. Class System.Net.NetworkInformation.NetworkInterface works for disconnected adatpers but can get only gataways Ips without metrics:

NetworkInterface nic = NetworkInterface.GetAllNetworkInterfaces().Where(e => e.Description == comboBoxInterface.Text).FirstOrDefault(); var nicProperties = nic.GetIPProperties(); var gateways = nicProperties.GatewayAddresses.Select(x => x.Address.ToString()).ToList();

  1. GetIpForwardTable also gets it only for connected interfaces.

How get it for disconnected interfaces, as in above commands?

Any other ways ?

Upvotes: 1

Views: 627

Answers (1)

vavtech
vavtech

Reputation: 41

Resolved using WMI:

ObjectQuery query = new ObjectQuery("SELECT NextHop, Metric1 FROM Win32_IP4RouteTable WHERE Destination='0.0.0.0' AND Metric1<>0 AND InterfaceIndex=" + selectedNetInterfaceIndex);

but next question how do it using API ?

Upvotes: 1

Related Questions