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. java基础(三) 加强型for循环与Iterator

    引言   从JDK1.5起,增加了加强型的for循环语法,也被称为 "for-Each 循环".加强型循环在操作数组与集合方面增加了很大的方便性.那么,加强型for循环是怎么解析的 ...

  2. wdcpV3面板安装ssl证书 apache教程 子站SSL配置

    本帖最后由 q1082121 于 2016-11-24 12:31 编辑 方案二 apache1.把apache类型的ssl三个文件上传到:/www/wdlinux//www/wdlinux/http ...

  3. iframe内存释放

    Ext 核心开发人员Jack的回答是,TabPanelItem在关闭时并不会对自定义到tab中的元素做特殊处理,这部分工作必须在控件外来完成.另一方面, 相关资料称IE在iframe元素的回收方面存在 ...

  4. socat 的神奇使用方式

    目的是实现科* 学 * 上*网,现在记录一下流程 先在服务器上安装(比如美国,香港,台湾,马来的云主机)squid,easy_rsa, centos 下可以用yum直接安装 $ yum install ...

  5. Oracle EBS 请求

    SELECT t.responsibility_id, t.responsibility_key, t.responsibility_name, t.description, t.menu_id, f ...

  6. Sql_从查询的结果集中分组后取最后有效的数据成新的结果集小记(待优化)

    Dim sql As String = " SELECT xp.*, " sql = sql + " xf_owner.ownername, " sql = s ...

  7. Oracle案例10——HWM(高水位线)性能优化

    最近BI同事反馈说一张表的数据查询非常慢,这个表数据总共不到1W行数据,这么一说我们首先想到的是高水位带来的性能问题,即高水位线下占用过多数据块,而这些数据块其实是部分数据占用,大多数是空闲的数据块. ...

  8. java StringBuilder案例

    实现输出字符串的长度,容量(容量不够则扩容),及内容 import java.util.Arrays; public class MyStringBuilderDemo { //任务:存储字符串并输出 ...

  9. debian 7上安装svn

    1.在终端中直接输入  sudo apt-get install subversion,选择安装即可 2.查看版本命令 svnserve --version(更多命令直接键入svnserve --he ...

  10. JNI学习笔记

    JNI是什么->一套c和java的互掉规则   为什么使用JNI 1.非常多敏感效率的代码已经用C实现了 2. JNI双向.java调用c,c调用java   Java集成本地代码问题 1.代码 ...