检测何时插入以太网电缆 [英] Detect when ethernet cable is plugged
问题描述
我正在尝试检测何时插入或拔出了以太网电缆,但是我遇到了一些问题,我不知道自己是否做得很好.
我正在使用NetworkChange.NetworkAddressChanged
检测网络何时发生更改
,然后按NetworkInterface.GetAllNetworkInterfaces()
键检查属性是否为.OperationalStatus
的以太网连接是否可用.
但是,当我在所有网络接口中搜索以太网连接时,它会返回我正在寻找的内容,但它会始终返回我与之建立的蓝牙连接.
这是代码:
public Form1()
{
InitializeComponent();
NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(AddressChangedCallback);
Console.ReadLine();
}
static void AddressChangedCallback(object sender, EventArgs e)
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface myInterface in adapters)
{
//if (n.Description.ToLower().Contains("ethernet")){
//if (n.NetworkInterfaceType.ToString().ToLower().Contains("ethernet")){
IPInterfaceProperties properties = n.GetIPProperties();
if (myInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
Console.WriteLine(myInterface.Description + " ........... : " + myInterface.OperationalStatus);
Console.WriteLine("NetworkInterfaceType : " + myInterface.NetworkInterfaceType);
}
}
}
在一开始,我试图检查连接的名称,并查看是否包含"Ethernet"一词,但有时会出现(如果我没记错的话),连接名称中不包含"Ethernet". /p>
您是否有一些技巧来始终保持良好的连接(没有蓝牙)?
我的方法错了吗?
我正在Surface Pro 3上对其进行测试...但是也许因为这个原因,我遇到了蓝牙问题? 尽管如此,我仍需要它在这样的设备上工作.
此链接显示了如何使用Powershell进行此操作,但其中一种情况是使用WMI.
http://www.powershellmagazine. com/2013/04/04/pstip-detecting-wi-fi-adapters/
此链接显示了一个有趣的属性,有时可以提供帮助:
https://msdn.microsoft. com/en-us/library/aa394217(v = vs.85).aspx
GatewayCostMetric
数据类型:uint16数组
访问类型:只读数组
整数成本度量值(范围从1到9999),用于 计算最快,最可靠或最少的资源消耗 路线.此参数与 DefaultIPGateway属性.
I'm trying to detect when an Ethernet cable is plugged-in or unplugged but i have some probleme and i don't know if i'm doing this good or not.
I'm using NetworkChange.NetworkAddressChanged
to detect when the network change
and then NetworkInterface.GetAllNetworkInterfaces()
for checking if Ethernet connexion is available or not with the property .OperationalStatus
.
But when i search for the Ethernet connexion in all the network interfaces, it return me what i'm looking for, but it always return me the Bluetooth connection with it.
Here is the code :
public Form1()
{
InitializeComponent();
NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(AddressChangedCallback);
Console.ReadLine();
}
static void AddressChangedCallback(object sender, EventArgs e)
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface myInterface in adapters)
{
//if (n.Description.ToLower().Contains("ethernet")){
//if (n.NetworkInterfaceType.ToString().ToLower().Contains("ethernet")){
IPInterfaceProperties properties = n.GetIPProperties();
if (myInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
Console.WriteLine(myInterface.Description + " ........... : " + myInterface.OperationalStatus);
Console.WriteLine("NetworkInterfaceType : " + myInterface.NetworkInterfaceType);
}
}
}
At the beginning, i was trying to check the name of the connection and looks if contains the "Ethernet" word, but it appears (if i'm not wrong) sometimes the connection name does not contain "Ethernet".
Do you have some tips for always bring the good connection (without the bluetooth)?
Am i wrong in my approach?
I'm testing it on a Surface Pro 3... but maybe i have the Bluetooth problem because of that? Despite that, i need it to work even on device like this.
This links shows how to do it with Powershell, but one of the cases uses WMI.
http://www.powershellmagazine.com/2013/04/04/pstip-detecting-wi-fi-adapters/
And this links shows a interesting property that can help sometimes:
https://msdn.microsoft.com/en-us/library/aa394217(v=vs.85).aspx
GatewayCostMetric
Data type: uint16 array
Access type: Read-only Array
of integer cost metric values (ranging from 1 to 9999) to be used in calculating the fastest, most reliable, or least resource-intensive routes. This argument has a one-to-one correspondence with the DefaultIPGateway property.
这篇关于检测何时插入以太网电缆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!