簡體   English   中英

C# 如何從禁用或斷開連接的網絡適配器(接口)獲取默認網關(路由)及其指標?

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

如何從禁用或斷開連接的網絡適配器(接口)獲取默認網關(路由)及其指標?

與使用“netsh int ip show address”或“route print”或“netsh interface ip show route”的結果相同,其持久性路由:

列出網關

  1. WMI Class Win32_NetworkAdapterConfiguration 僅適用於啟用和連接的適配器:

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

  1. Class System.Net.NetworkInformation.NetworkInterface 適用於斷開連接的適配器,但只能獲得沒有指標的網關 Ips:

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 也只為連接的接口獲取它。

如上面的命令,如何為斷開的接口獲取它?

還有其他方法嗎?

使用 WMI 解決:

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

但下一個問題是如何使用 API?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM