public void GetPhysicsNetworkAdapterInfo()
{
ManagementObjectSearcher mos = new ManagementObjectSearcher(@"\\.\ROOT\StandardCimv2", "SELECT * FROM MSFT_NetAdapter WHERE Virtual=False");
ManagementObjectCollection moc = mos.Get();
foreach (ManagementObject mo in moc)
{
string driverDescription = mo["DriverDescription"]?.ToString();
uint interfaceType = Convert.ToUInt32(mo["InterfaceType"] ?? 0);
string macAdddr = mo["PermanentAddress"]?.ToString();
Console.WriteLine("---------------------------------------");
Console.WriteLine($"DriverDescription:{driverDescription}");
Console.WriteLine($"MacAddr:{macAdddr}");
Console.WriteLine($"InterfaceType:{interfaceType}");
}
}根据上面的结果对下面的进行过滤然后就可以实现网卡切换
void DisplayIPv4NetworkInterfaces()
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
Console.WriteLine("IPv4 interface information for {0}.{1}",
properties.HostName, properties.DomainName);
Console.WriteLine();
foreach (NetworkInterface adapter in nics)
{
// Only display informatin for interfaces that support IPv4.
if (adapter.Supports(NetworkInterfaceComponent.IPv4) == false)
{
continue;
}
if (adapter.NetworkInterfaceType != NetworkInterfaceType.Wireless80211)
{
continue;
}
Console.WriteLine(adapter.Description);
// Underline the description.
Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length, '='));
IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
// Try to get the IPv4 interface properties.
IPv4InterfaceProperties p = adapterProperties.GetIPv4Properties();
if (p == null)
{
Console.WriteLine("No IPv4 information is available for this interface.");
Console.WriteLine();
continue;
}
// Display the IPv4 specific data.
Console.WriteLine(" Index ............................. : {0}", p.Index);
Console.WriteLine(" MTU ............................... : {0}", p.Mtu);
Console.WriteLine(" APIPA active....................... : {0}",
p.IsAutomaticPrivateAddressingActive);
Console.WriteLine(" APIPA enabled...................... : {0}",
p.IsAutomaticPrivateAddressingEnabled);
Console.WriteLine(" Forwarding enabled................. : {0}",
p.IsForwardingEnabled);
Console.WriteLine(" Uses WINS ......................... : {0}",
p.UsesWins);
Console.WriteLine(" Id ............................. : {0}", adapter.Id); // 获取网络适配器的标识符
Console.WriteLine(" Name............................. : {0}", adapter.Name); // 获取网络适配器的名称
Console.WriteLine(" Description ......... : {0}", adapter.Description); // 获取接口的描述
Console.WriteLine(" Interface type ...... : {0}", adapter.NetworkInterfaceType); // 获取接口类型
Console.WriteLine(" Is receive only...... : {0}", adapter.IsReceiveOnly); // 获取 Boolean 值,该值指示网络接口是否设置为仅接收数据包。
Console.WriteLine(" Multicast............ : {0}", adapter.SupportsMulticast); // 获取 Boolean 值,该值指示是否启用网络接口以接收多路广播数据包。
Console.WriteLine(" Speed ............... : {0}", adapter.Speed); // 网络接口的速度
Console.WriteLine(" Physical Address .... : {0}", adapter.GetPhysicalAddress().ToString()); // MAC 地址
Console.WriteLine();
}
}
评论区