c# 区分虚拟网卡 获取真实网卡信息 判断网卡有线或无线

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();
    }
}

MSFT_NetAdapter所有成员

InterfaceType(可用于判断网卡为有线或无线)


本文作者:admin

本文链接:https://www.javalc.com/post/87.html

版权声明:本篇文章于2022-01-06,由admin发表,转载请注明出处:分享你我。如有疑问,请联系我们

.net core 内置的 System.Text.Json 序列化类型扩展支持

发表评论

取消
扫码支持