识别活动网卡并获取默认网关

时间:2013-07-31 17:28:07

标签: c# visual-studio-2010 c#-4.0

如何获取活动网卡的标识并获取默认网关?

http://s4.picofile.com/file/7871762682/Default_Gateway.png

1 个答案:

答案 0 :(得分:3)

以下代码为您提供了第一个默认网关:

 NetworkInterface card = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault();
 if (card == null)
    return null;
 GatewayIPAddressInformation address = card.GetIPProperties().GatewayAddresses.FirstOrDefault();
 if (address == null)
     return null;

 return address.Address;