简体   繁体   English

如何确定使用哪个适配器?

[英]How do you determine which adapter is used?

I have a need to figure out which adapter is used when a connection is created. 我需要弄清楚创建连接时使用的适配器。 In other words, if I have multiple NIC cards (ie wireless, lan, etc) on my machine, which card is being used for the connection? 换句话说,如果我的机器上有多个NIC卡(即无线,局域网等),哪个卡用于连接?

If anyone can point me in the right direction... 如果有人能指出我正确的方向......

In C# 在C#中

foreach(var nic in NetworkInterface.GetAllNetworkInterfaces.Where(n => n.OperationalStatus == OperationStatus.UP)
{
    if(nic.GetIsNetworkAvailable())
    {
       //nic is attached to some form of network
    }
}

VB .NET VB .NET

ForEach nic in NetworkInterface.GetAllNetworkInterfaces.Where(Function(n) n.OperationalStatus = OperationStatus.UP)
    If nic.GetIsNetworkAvailable() Then
       //nic is attached to some form of network
    End If
Next

This will only test active working Network Interfaces that are connected to an active network. 这将仅测试连接到活动网络的活动工作网络接口。

你为什么不使用MAC地址?

Maybe you could map it by the MAC Address: 也许你可以通过MAC地址映射它:

var nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (var nic in nics)
{
    if (nic.OperationalStatus == OperationalStatus.Up)
    {
        var mac = nic.GetPhysicalAddress().ToString();
        if (mac == "your:connections:mac:address")
        {
            /* ... */
        }
    }
}

The "your:connections:mac:address" part you can figure out following this method, using the IP address of the LocalEndPoint . 使用LocalEndPoint的IP地址,可以使用此方法找出“your:connections:mac:address”部分。

How do I obtain the physical (MAC) address of an IP address using C#? 如何使用C#获取IP地址的物理(MAC)地址?

It's not beautiful, but it could work. 它不漂亮,但它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 你如何确定哪些库没有被使用 - How do you determine which libraries are not being used 如何确定我的计算机正在使用哪个网络适配器? - How do I determine which network adapter my computer is using? 如何确定哪个网络适配器连接到Internet - How to determine which network adapter is connected to the internet 如何确定使用哪种身份验证方法? - How to determine which authentication method is used? 如何确定正在使用哪些单位? - How to determine which units are being used? 调用View(Object model)时如何确定使用哪个View - How do I determine which View is used when calling View(Object model) 在图形中,如何确定两个顶点是否形成桥,一旦断开桥,哪些顶点将成为哪个子图的一部分? - In a graph, how do you determine if two vertices form a bridge and which vertices will be part of which subgraph once the bridge is disconnected? 如何确定是否选择了某个项目? - How do you determine if an item is selected? 您如何测试私有方法内部的异常,该方法由公共 void 使用? - How do you test for an exception that is inside of a private method, which is used by a public void? 确定进程正在使用的网络适配器 - Determine which network adapter a process is using
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM