[英]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.