我需要一个事件来检测互联网连接/断开连接
我们正在开发一个 .NET 应用程序,其中的要求之一是监视系统是否连接到互联网。
我们能够获得“以太网电缆断开连接”的 .NET 事件,但如果重置调制解调器,则不会触发该事件。我不想继续 ping 某些 URL 来完成此操作,因为这会增加相当大的性能开销。是否有任何.NET事件可以检查系统是否连接到互联网?
当系统未连接到互联网时,系统托盘中有一个图标,该图标显示十字标志或有限连接标志。这表明 Windows 知道,并且我想捕获该事件。
We are developing a .NET application where one of the requirements is to monitor whether the system is connected to the internet or not.
We were able to get a .NET event for "ethernet cable disconnect", but if the modem is reset then this event does not get triggered. I don't want to keep pinging some URL to get this done since it will add considerable performance overhead. Is there any .NET event which can check whether the system is connected to the internet or not?
There is an icon in system tray which shows a cross sign or limited connectivity sign when the system is not conncected to the internet. That suggests Windows knows, and I want to trap that event.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 NetworkChange 类,其中NetworkAvailabilityChanged 事件:
由于它是系统事件,因此请确保在完成后删除该事件,请参阅此处的这篇文章:您需要小心使用 NetworkChange 事件处理程序
You can use the NetworkChange class, with the NetworkAvailabilityChanged event:
Since it's a system event, make sure you delete the event when you're finished, see this post here: You need to be careful about using event handler for NetworkChange
http://msdn.microsoft.com/en-us/library/ee264321(VS.85).aspx。我希望您打算在您的帖子中添加 Windows 7 标签,因为这一切都是全新的。
关键是
INetworkListManager.get_IsConnectedToInternet()
,它几乎按照其表面上的说明进行操作。您必须稍微跳转一下才能注册事件等。 代码包 包含其中的一些内容为您准备并提供您可以适应的网络示例。This is all covered (including the difference between being on the network and having the network connect you to the Internet) at http://msdn.microsoft.com/en-us/library/ee264321(VS.85).aspx. I hope you meant to put that Windows 7 tag on your post, because all this is pretty new.
The key is
INetworkListManager.get_IsConnectedToInternet()
which pretty much does what it says on the tin. You have to jump around a bit to register for the events etc. The Code Pack wraps some of that up for you and has a network sample you can adapt.我能够在一定程度上解决这个问题。我能够在代码项目中找到一些示例代码
http://www.codeproject.com/script/Articles/ListVersions。 aspx?aid=34650。谢谢大家的回复。
尤其是格雷戈里女士发布的文章链接对我帮助很大。
I was able to solve this problem to some extent. I was able to find some sample code in Code project
http://www.codeproject.com/script/Articles/ListVersions.aspx?aid=34650. Thanks all for the replies.
especially the article link which was posted by Ms Gregory helped me a lot.
这对我有用!
This worked for me!
试试这个:
try with this:
你必须使用WMI。
这是断开连接检测的示例:(为了测试它,创建一个 .vbs 文件并运行它)
you have to use WMI .
This is example for disconnect detection : ( to test it create a .vbs file and run it )