Hi,
The Offline Application Block provides a mechanism to determine if you
have an internet connection. Here is the class that implements this:
using System;
using System.Runtime. InteropServices ;
using System.Xml.XPat h;
using System.Configur ation;
using Microsoft.Appli cationBlocks.Co mmon;
namespace Microsoft.Appli cationBlocks.Sm artClient.Offli ne
{
/// <summary>
/// This is a simple implementation of a ConnectionDetec tionStrategy.
It uses the WinINet
/// function GetInternetConn ectionState to detect whether you are
actually connected to
/// another system anywhere. This strategy is not smart enough to tell
if you can talk
/// to a particular host -- it merely checks to see that you can talk
to *any* host.
/// This means that there is an ethernet connection to another
computer or a network,
/// or that a modem is connected.
/// </summary>
public class WinINetDetectio nStrategy
{
private int pollInterval;
/// <summary>
/// Public constant used in test code.
/// </summary>
public const int MinimumPollingI ntervalInSecond s = 1;
[DllImport("wini net.dll")]
private extern static bool InternetGetConn ectedState( out int
connectionDescr iption, int reservedValue ) ;
/// <summary>
/// Getter property to retrieve the polling interval in seconds
/// </summary>
/// <value>Pollin g interaval in seconds</value>
public int PollInterval { get { return pollInterval; }}
/// <summary>
/// Query method to cause provider to actively detect the connection
state
/// </summary>
/// <returns>True if provider believes we are connected</returns>
public bool IsConnected()
{
int connectionDescr iption = 0;
return InternetGetConn ectedState(out connectionDescr iption, 0);
}
/// <summary>
/// IProvider.Initi alize method implementation. Retrieves
configuration information from app.config file
/// </summary>
/// <param name="configura tionNode">XmlNo de to parse for
configuration information</param>
public void Initialize(XPat hNavigator configurationNo de)
{
XPathNodeIterat or iter =
configurationNo de.Select("poll ingInterval");
iter.MoveNext() ;
pollInterval = GetCurrentPolli ngInterval(iter .Current.Value) ;
if(pollInterval < MinimumPollingI ntervalInSecond s)
{
throw new
ConfigurationEx ception(Provide rsResourceTable .GetString(Prov idersResourceTa ble.ConnectionM anagerPollingIn tervalMessage)) ;
}
}
private int GetCurrentPolli ngInterval(stri ng pollingInterval String)
{
try
{
return Convert.ToInt32 (pollingInterva lString);
}
catch(FormatExc eption e)
{
throw new
ConfigurationEx ception(Provide rsResourceTable .GetString(Prov idersResourceTa ble.PollingInte rvalShouldBeNum eric),
e);
}
}
}
}
If you use this class, then you'll want to call the IsConnected()
method to determine connectivety. You can learn more about the Offline
block at:
http://msdn.microsoft.com/library/de...fline-ch01.asp
Thanks,
Sayed Y. Hashimi
http://www.sayedhashimi.com
Shameless Book Plug: Service-Oriented Smart Clients with .NET 2.0
http://www.amazon.com/exec/obidos/tg...glance&s=books