Deteccion de conectividad

Solo disponible en BuenasTareas
  • Páginas : 2 (278 palabras )
  • Descarga(s) : 0
  • Publicado : 1 de febrero de 2011
Leer documento completo
Vista previa del texto
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.NetworkInformation;

public class DetectarConectividad
{
public static voidMain()
{
Console.WriteLine(IsConnected);
Console.WriteLine(IsConnected2);
Console.WriteLine(IsConnected3);
Console.WriteLine("Ahora vigilamos posibles cambios en la conectividad medianteel uso de eventos");
NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(AddressChangedCallback);
NetworkChange.NetworkAvailabilityChanged += newNetworkAvailabilityChangedEventHandler(AvailabilityChangedCallback);
Console.ReadLine();
}

//Método 1: API de Iternet de Windows
//http://msdn2.microsoft.com/en-us/library/aa384702.aspx[DllImport("wininet.dll")]
private static extern bool InternetGetConnectedState(ref uint flags, uint dwReserved);

private const int INTERNET_CONNECTION_CONFIGURED = 0x40; //Conexión a Internet configurada, pero notiene porqué estar activa
private const int INTERNET_CONNECTION_LAN = 0x02; //Se usa la red local para salir a Internet
private const int INTERNET_CONNECTION_MODEM = 0x01; //se usa un módem
privateconst int INTERNET_CONNECTION_PROXY = 0x04; //Se sale a través de un proxy

public static bool IsConnected
{
get
{
uint flags = 0;
return InternetGetConnectedState(ref flags,0);}
}

//Método 2: System Event Notification Service (SENS API)
//http://msdn.microsoft.com/library/default.asp?url=/library/en-us/syncmgr/syncmgr/isnetworkalive.asp[DllImport("sensapi.dll")]
private static extern bool IsNetworkAlive(ref uint flags);
private const int NETWORK_ALIVE_LAN = 0x00000001;
private const int NETWORK_ALIVE_WAN = 0x00000002;


public static boolIsConnected2
{
get
{
uint flags = 0;
return IsNetworkAlive(ref flags);
}
}

public static bool IsConnected3
{
get
{
try
{
string hostName =...
tracking img