我需要一个事件来检测互联网连接/断开连接

发布于 2024-10-08 12:35:01 字数 243 浏览 2 评论 0原文

我们正在开发一个 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦初启 2024-10-15 12:35:01

您可以使用 NetworkChange 类,其中NetworkAvailabilityChanged 事件:

NetworkChange.NetworkAvailabilityChanged += myNetworkAvailabilityChangeHandler;

由于它是系统事件,因此请确保在完成后删除该事件,请参阅此处的这篇文章:您需要小心使用 NetworkChange 事件处理程序

You can use the NetworkChange class, with the NetworkAvailabilityChanged event:

NetworkChange.NetworkAvailabilityChanged += myNetworkAvailabilityChangeHandler;

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

唯憾梦倾城 2024-10-15 12:35:01

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.

过潦 2024-10-15 12:35:01

我能够在一定程度上解决这个问题。我能够在代码项目中找到一些示例代码
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.

酷遇一生 2024-10-15 12:35:01

这对我有用!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.NetworkInformation;
namespace ConsoleApplication6
{


    class Program
    {
        private void AvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {

            if (e.IsAvailable)
                Console.WriteLine("Network connected!");
            else
                Console.WriteLine("Network dis connected!");
        }
        public void Form1()
        {

            NetworkChange.NetworkAvailabilityChanged += AvailabilityChanged;
        }

        static void Main(string[] args)
        {
            Program p = new Program();

            p.Form1();

            Console.ReadLine();

        }
    }
}

This worked for me!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.NetworkInformation;
namespace ConsoleApplication6
{


    class Program
    {
        private void AvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {

            if (e.IsAvailable)
                Console.WriteLine("Network connected!");
            else
                Console.WriteLine("Network dis connected!");
        }
        public void Form1()
        {

            NetworkChange.NetworkAvailabilityChanged += AvailabilityChanged;
        }

        static void Main(string[] args)
        {
            Program p = new Program();

            p.Form1();

            Console.ReadLine();

        }
    }
}
皇甫轩 2024-10-15 12:35:01

试试这个:

private void AvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            if (e.IsAvailable)
                Console.WriteLine("Wi-Fi conectado " + DateTime.Now );
            else
                Console.WriteLine("Wi-Fi desconectado " + DateTime.Now);
        }


        public Inicio()
        {
            InitializeComponent();

            NetworkAvailabilityChangedEventHandler myHandler = new NetworkAvailabilityChangedEventHandler(AvailabilityChanged);
            NetworkChange.NetworkAvailabilityChanged += myHandler;
        }

try with this:

private void AvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            if (e.IsAvailable)
                Console.WriteLine("Wi-Fi conectado " + DateTime.Now );
            else
                Console.WriteLine("Wi-Fi desconectado " + DateTime.Now);
        }


        public Inicio()
        {
            InitializeComponent();

            NetworkAvailabilityChangedEventHandler myHandler = new NetworkAvailabilityChangedEventHandler(AvailabilityChanged);
            NetworkChange.NetworkAvailabilityChanged += myHandler;
        }
迷鸟归林 2024-10-15 12:35:01

你必须使用WMI。

这是断开连接检测的示例:(为了测试它,创建一个 .vbs 文件并运行它)

 strComputer = "."

  Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\wmi")
  Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("Select * from MSNdis_StatusMediaDisconnect") 

  Do While True 
    Set strLatestEvent = colMonitoredEvents.NextEvent 
    Wscript.StdOut.Write "A network connection has been lost: " 
    Wscript.StdOut.Writeline strLatestEvent.InstanceName
  Loop

you have to use WMI .

This is example for disconnect detection : ( to test it create a .vbs file and run it )

 strComputer = "."

  Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\wmi")
  Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("Select * from MSNdis_StatusMediaDisconnect") 

  Do While True 
    Set strLatestEvent = colMonitoredEvents.NextEvent 
    Wscript.StdOut.Write "A network connection has been lost: " 
    Wscript.StdOut.Writeline strLatestEvent.InstanceName
  Loop
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文