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

Detect Changes in Network Connectivity的更多相关文章

  1. ionic_ Network connectivity error occurred, are you offline?

    错误如下: HenHouse admin$ ionic cordova build ios --prod > ionic integrations enable cordova ✖ Downlo ...

  2. Coursera Algorithms week1 查并集 练习测验:1 Social network connectivity

    题目原文描述: Given a social network containing. n members and a log file containing m timestamps at which ...

  3. [转]Peer-to-Peer Communication Across Network Address Translators

    Peer-to-Peer Communication Across Network Address Translators Bryan Ford Massachusetts Institute of ...

  4. Android开发训练之第五章——Building Apps with Connectivity & the Cloud

    Building Apps with Connectivity & the Cloud These classes teach you how to connect your app to t ...

  5. RabbitMQ Network Partitions

    Clustering and Network Partitions RabbitMQ clusters do not tolerate network partitions well. If you ...

  6. Managing Network Usage

    This lesson describes how to write applications that have fine-grained control over their usage of n ...

  7. Configure a bridged network interface for KVM using RHEL 5.4 or later?

    environment Red Hat Enterprise Linux 5.4 or later Red Hat Enterprise Linux 6.0 or later KVM virtual ...

  8. Simple Network Management Protocol - SNMP Tutorial

    30.9 Simple Network Management Protocol Network management protocols specify communication between t ...

  9. How Network Load Balancing Technology Works--reference

    http://technet.microsoft.com/en-us/library/cc756878(v=ws.10).aspx In this section Network Load Balan ...

随机推荐

  1. textarea高度跟随文字高度而变化

    html部分: <textarea id="textarea">哈喽哈喽哈喽哈喽哈喽哈喽哈喽哈喽哈喽哈喽哈喽哈喽</textarea> js部分: < ...

  2. Windows平台使用Gitblit搭建Git服务器教程

    Windows平台使用Gitblit搭建Git服务器图文教程 Git服务现在独树一帜,相比与SVN有更多的灵活性,最流行的开源项目托管网站Github上面,如果托管开源项目,那么就是免费使用的,但是闭 ...

  3. 通过nginx 访问 centos 7 服务器上的.Net Core

    先安装依赖 # yum -y install pcre-devel openssl openssl-devel # yum -y install gcc gcc-c++ autoconf automa ...

  4. 我遇到的问题:耗时久/效率低 ---> 应对方案: 行动-结果指向

    这一篇打的时候,时间都挺靠后的了, 当时出现错误,很慌了,一个是时间比较久,5点多了,一个是陈果已经做了很多题了,这些是事实. 导致我慌张的原因,简单来说是比较,长久以来,我都爱去和别人比较.如果赢了 ...

  5. mysql5.7.22的安装与配置(适用mysql5.7.20至mysql5.7.22版本)

    一.解压Mysql5.7.20安装包,刚解压是没有  data  这个文件夹的 二.配置mysql环境变量,创建MYSQL_HOME,然后在Path上添加%MYSQL_HOME%\bin; 三.配置m ...

  6. 2015 ICL, Finals, Div. 2【ABFGJK】

    [题外话:我......不补了......] 2015 ICL, Finals, Div. 2:http://codeforces.com/gym/100637 G. #TheDress[水] (st ...

  7. vim高级操作命令

    1.首先在命令模式下,输入“:set nu”显示行号:通过行号确定你要删除的行:命令输入“:32,65d”,回车键,32-65行就被删除了,很快捷吧如果无意中删除错了,可以使用‘u’键恢复(命令模式下 ...

  8. 智能家居中的物联网网关的可信计算平台模块(TPM)设计

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/BlueCloudMatrix/article/details/24184461 摘要: 随着智能家居 ...

  9. [AHOI2009]飞行棋

    嘟嘟嘟 刚开始想这道题的时候确实很蒙,只想到矩形对边做对应的弧长相等,然后想办法凑出相等的弧长.其实正解很简单,不要去想边,应该想对角线,因为根据初中园的知识,这个矩形的对角线是圆的直径,而直径所对的 ...

  10. 在任务管理器中显示所有CPU内核性能

    在Windows7"任务管理器"的”性能“选项卡默认显示所有的CPU内核性能 在Windows10中可以通过设置来实现效果