zoukankan      html  css  js  c++  java
  • Detect Changes in Network Connectivity

    Some times you will need a mechanism to check whether changes to network occurring during running your application.
    So as a solution for this you can add handlers to the static NetworkAddressChanged and NetworkAvailabilityChanged events implemented by theSystem.Net.NetworkInformation.NetworkChange class.

    Souce Code:

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net.NetworkInformation;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Add the handlers to the NetworkChange events.
                NetworkChange.NetworkAvailabilityChanged +=
                NetworkAvailabilityChanged;
                NetworkChange.NetworkAddressChanged +=
                NetworkAddressChanged;

                Console.ReadLine();
            }

            // Declare a method to handle NetworkAvailabilityChanged events.
            private static void NetworkAvailabilityChanged(
            object sender, NetworkAvailabilityEventArgs e)
            {
                // Report whether the network is now available or unavailable.
                if (e.IsAvailable)
                {
                    Console.WriteLine("Network Available");
                }
                else
                {
                    Console.WriteLine("Network Unavailable");
                }
            }
            // Declare a method to handle NetworkAdressChanged events.
            private static void NetworkAddressChanged(object sender, EventArgs e)
            {
                Console.WriteLine("Current IP Addresses:");
                // Iterate through the interfaces and display information.
                foreach (NetworkInterface ni in
                NetworkInterface.GetAllNetworkInterfaces())
                {
                    foreach (UnicastIPAddressInformation addr
                    in ni.GetIPProperties().UnicastAddresses)
                    {
                        Console.WriteLine(" - {0} (lease expires {1})",
                        addr.Address, DateTime.Now +
                        new TimeSpan(0, 0, (int)addr.DhcpLeaseLifetime));
                    }
                }
            }
        }
    }


    http://www.codeproject.com/Articles/206720/Simple-Network-Status-Monitor-Example

  • 相关阅读:
    线段树【加强】
    ATM(BZOJ 1179)
    Trick or Treat on the Farm
    欧拉回路 HDU
    无序字母对(luogu 1314)
    MooFest
    Snowflake Snow Snowflakes(POJ 3349)
    Firetruck(UVA 208)
    B进制星球(luogu 1604)
    遍历一个树的所有子节点,画出该树,深度不定,广度不定,适用于任何树,深度优先算法
  • 原文地址:https://www.cnblogs.com/zyip/p/5192421.html
Copyright © 2011-2022 走看看