I need a event to detect Internet connect/disconnect

前端 未结 6 2033
清酒与你
清酒与你 2020-11-27 13:44

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

6条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 14:40

    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();
    
            }
        }
    }
    

提交回复
热议问题