请教,怎么使用C#获取本地网关的IP和MAC地址

2024-12-17 08:18:26
推荐回答(1个)
回答1:

using System.Management;
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection nics = mc.GetInstances();
foreach (ManagementObject nic in nics)
{
if (Convert.ToBoolean(nic["ipEnabled"]) == true)
{
string mac = nic["MacAddress"].ToString();//Mac地址
string ip = (nic["IPAddress"] as String[])[0];//IP地址
string ipsubnet = (nic["IPSubnet"] as String[])[0];//子网掩码
string ipgateway = (nic["DefaultIPGateway"] as String[])[0];//默认网关
}
}