当Internet连接或断开时发出通知 [英] Get notified when internet is connected or disconnected

查看:136
本文介绍了当Internet连接或断开时发出通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有.NET 4.0客户端
有以显示或根据如果Internet是否可用在PC上隐藏一些数据的框架下开发WPF和C#桌面应用程序。
这意味着我必须能够尽可能快检测是否在互联网断开或连接,以便发生反应。 (例子可以从无线网络断开连接或拔掉网线)



作为我研究了一下,我发现一个事件类中存在 NetworkChange 名为 NetworkAvailabilityChanged ,每一次火灾的连接改变(的链接文档)。



我在Windows 8开发的机器,当我尝试它有没有触发事件,但是当我在Windows 7上运行应用程序的事件。由于在文档与该事件的框架4.0的兼容性说,直到Windows 7中,但是看着在文档为框架4.5的兼容性同一事件称Windows 8也。



现在的问题是,即使我切换项目的目标框架4.5(这不是想法)事件不会触发在Windows 8。



是那里,我并没有意识到任何不兼容的问题?有没有更好的方式来获得通知,当连接的变化?



我需要的,如果可能的框架4.0客户端,在Windows Vista中,7和8 <有效的解决方案/ p>

解决方案

@William莱利的answe帮我找去做的方式。



最后我来使用网络列表管理器通过从Microsoft的COM实现在Windows API的地步。 (你可以检查一些范例





添加正确的参考项目是非常重要的=在这里输入的形象描述>



然后就可以按照从链接的例子,实在是短而直的地步。


I have a desktop application in WPF and C# developed under the .Net 4.0 Client Framework that has to show or hide some data according to if internet is available or not on the PC. This means I have to be able to detect as soon as possible if the internet is disconnected or connected in order to react. (Example could be disconnect from a wireless network or unplugging the network cable)

As I researched a bit I found that an event exists in the class NetworkChange called NetworkAvailabilityChanged that fires every time the connection changes (link to docs).

I'm developing in a Windows 8 machine and when I try it there the event is not fired but when I run the app on Windows 7 the event is fired. As in the docs says with the framework 4.0 compatibility for that event is until Windows 7, but looking at the same event in the docs for framework 4.5 compatibility says windows 8 also.

The problem is that even if I switch the target framework of the project to 4.5 (which is not the idea) the event won't fire on Windows 8.

Are there any incompatibilities issues that I'm not realizing? Is there a better way to get notified when the connection changes?

I need if possible a solution for the Framework 4.0 client that works on Windows Vista, 7 and 8

解决方案

The answe of @William Riley help me to find the way to do it.

Finally I come to the point using the Network List Manager from the Windows API through the COM implementation from Microsoft. (you can check some examples)

It is important to add the correct reference to the project:

Then just you can follow the example from the link, is really short and direct to the point.

这篇关于当Internet连接或断开时发出通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆