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
Detect Changes in Network Connectivity的更多相关文章
- ionic_ Network connectivity error occurred, are you offline?
错误如下: HenHouse admin$ ionic cordova build ios --prod > ionic integrations enable cordova ✖ Downlo ...
- Coursera Algorithms week1 查并集 练习测验:1 Social network connectivity
题目原文描述: Given a social network containing. n members and a log file containing m timestamps at which ...
- [转]Peer-to-Peer Communication Across Network Address Translators
Peer-to-Peer Communication Across Network Address Translators Bryan Ford Massachusetts Institute of ...
- Android开发训练之第五章——Building Apps with Connectivity & the Cloud
Building Apps with Connectivity & the Cloud These classes teach you how to connect your app to t ...
- RabbitMQ Network Partitions
Clustering and Network Partitions RabbitMQ clusters do not tolerate network partitions well. If you ...
- Managing Network Usage
This lesson describes how to write applications that have fine-grained control over their usage of n ...
- 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 ...
- Simple Network Management Protocol - SNMP Tutorial
30.9 Simple Network Management Protocol Network management protocols specify communication between t ...
- 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 ...
随机推荐
- 用Spider引擎解决数据库垂直和水平拆分的问题
作者介绍 张秀云,网名飞鸿无痕,现任职于腾讯,负责腾讯金融数据库的运维和优化工作.2007年开始从事运维方面的工作,经历过网络管理员.Linux运维工程师.DBA.分布式存储运维等多个IT职位.对Li ...
- [翻译] ValueTrackingSlider
ValueTrackingSlider What is it? A UISlider Subclass that displays live values in a popUpView. It’s i ...
- Python入门-模块1(模块导入与time模块)
---恢复内容开始--- 模块 一.模块分类: 模块分为三种: 1.内置模块:Python自带的标准模块(可使用help('modules’)查看Python自带模块列表) 2.第三方开源模块:可以通 ...
- Linux 系统的/etc目录
/etc目录下的重要文件 /etc/sysconfig/network 指定服务器上的网络配置信息 /etc/rc.d/init.d/network 网络配置脚本信息 网络配置脚本,开机通过脚本文件来 ...
- 安全紧急预警-防范新型 Sigrun 勒索病毒
近日,互联网上出现一种 Sigrun 勒索病毒,其通过垃 圾邮件.网站捆绑软件等方式进行传播.该病毒一旦植入到 用户的服务器,将把系统文件加密为.sigrun 的文件,进而 向受害者勒索虚拟货币.该新 ...
- redis 配置文件redis.conf
daemonize yes #---默认值no,该参数用于定制redis服务是否以守护模式运行.--- pidfile /var/run/redis.pid #默认值/var/run/redis.pi ...
- angular 生命周期钩子 ngOnInit() 和 ngAfterViewInit() 的区别
angular 生命周期钩子的详细介绍在 https://angular.cn/guide/lifecycle-hooks 文档中做了介绍. ngOnInit() 在 Angular 第一次显示数据 ...
- Spring-IOC bean 生命周期之 Lifecycle 钩子
Lifecycle callbacks Initialization callbacks.Destruction callbacks 要与容器的bean生命周期管理交互,即容器在启动后和容器在销毁前对 ...
- Installing Oracle Database 12c Release 2(12.2) RAC on RHEL7.3 in Silent Mode
概要 在RHEL7静默方式安装oracle database 12.2 RAC. 一.环境配置 1. 配置hosts文件 cp /etc/hosts /etc/hosts_$(date +%Y%d%m ...
- Java进阶 -- 文章汇总
文章汇总 Java集合源码 -- Collection框架概述 Java集合源码 -- Map映射和Set集合 Java集合源码 -- List列表 String和StringBuffer 内部类 j ...