C# NetworkAvailabilitychangedEvent
VS 2008
我正在开发一个应用程序,它必须检测客户端是否有网络连接。 即 LAN 电缆已插入或无线已打开。 我一直在使用下面的代码。
我正在使用 NetworkAvailabilitychangedEvent 在无线关闭或电缆被拔出时触发事件。 但是,这仅在用户只有 3 个连接(LAN、无线和环回)时才有效。
微软:“当至少一个网络接口被标记为“启动”并且不是隧道或环回接口时,网络可用”。
然而,有些客户端有超过 3 个连接。 一个客户端有蓝牙连接,其他客户端有一些 VMWare 连接。 在这些客户端上,它未能触发事件。
无论如何,我是否可以忽略所有这些我不感兴趣的连接,而只在局域网和无线上监听?
非常感谢您的任何建议,
private void Form1_Load(object sender, EventArgs e)
{
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(OnNetworkChangedEvent);
}
private void OnNetworkChangedEvent(object sender, NetworkAvailabilityEventArgs e)
{
bool available = e.IsAvailable;
NetworkInterface[] networkConnections = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in networkConnections)
{
if (ni.Name == "Local Area Connection")
{
if (ni.OperationalStatus == OperationalStatus.Down)
{
Console.WriteLine("LAN disconnected: " + ni.Description);
}
}
else if (ni.Name == "Wireless Network Connection")
{
if (ni.OperationalStatus == OperationalStatus.Down)
{
Console.WriteLine("Wireless disconnected: " + ni.Description);
}
}
}
}
VS 2008
I am developing an application that has to detect whether a client has a network connection. i.e. LAN cable plugged in or wireless switched on. I have been using the code below.
I am using the NetworkAvailabilitychangedEvent to fire an event when their wireless is turned off or the cable has been pulled out. However, this only works if the user has only 3 connections present (LAN, Wireless, and loopbacks).
Microsoft: "The network is available when at least one network interface is marked "up" and is not a tunnel or loopback interface".
However, some client has more than 3 connections. One client had a bluetooth connection and someone else had some VMWare connections. On these client it failed to fire the event.
Is there anyway I can ignore all these connections I am not interested in listening out for, and just listen on the LAN and Wireless?
Many thanks for any advice,
private void Form1_Load(object sender, EventArgs e)
{
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(OnNetworkChangedEvent);
}
private void OnNetworkChangedEvent(object sender, NetworkAvailabilityEventArgs e)
{
bool available = e.IsAvailable;
NetworkInterface[] networkConnections = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in networkConnections)
{
if (ni.Name == "Local Area Connection")
{
if (ni.OperationalStatus == OperationalStatus.Down)
{
Console.WriteLine("LAN disconnected: " + ni.Description);
}
}
else if (ni.Name == "Wireless Network Connection")
{
if (ni.OperationalStatus == OperationalStatus.Down)
{
Console.WriteLine("Wireless disconnected: " + ni.Description);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您需要实现某种在单独线程上运行的轮询代理,并定期尝试联系远程服务器(或多个服务器?)上侦听的内容。
当它失败时(或者可能是当预设数量的连接尝试失败时),然后触发一个事件。 当它从失败-成功状态转变时也是如此。
您的应用程序将订阅这些事件并采取相应的操作。
当然这可能不适合你的场景..
Perhaps you need to implement some sort of polling agent that runs on a seperate thread and regularly tries to contact something listening on a remote server (or servers?).
When it fails (or perhaps when a preset number of connection attempts fail) then fire an event. Same when it goes from a fail-succeed state.
Your application would subscribe to these events and take action accordingly.
Of course this may not be suitable for your scenario..