1: /// <summary>
2: /// Performs actions on the network
3: /// </summary>
4: public class NetworkHandler
5: { 6: // Extern Library
7: // UnManaged code - be careful.
8: [DllImport("wininet.dll")] 9: private extern static bool
10: InternetGetConnectedState(out int Description, int ReservedValue);
11:
12: /// <summary>
13: /// Determines if there is an active connection on this computer
14: /// </summary>
15: /// <returns></returns>
16: public static bool HasActiveConnection()
17: { 18: try
19: { 20: int desc;
21: return InternetGetConnectedState(out desc, 0);
22: }
23: catch { } 24:
25: // If it gets here, then wininet.dll must have failed, so
26: // try resolving a host that is always going to be online such as google, microsoft
27: System.Uri Url = new System.Uri("http://www.google.com"); 28: System.Net.WebRequest WebReq;
29: System.Net.WebResponse Resp;
30: WebReq = System.Net.WebRequest.Create(Url);
31: try
32: { 33: Resp = WebReq.GetResponse();
34: Resp.Close();
35: WebReq = null;
36: return true;
37: }
38: catch {} 39: finally
40: { 41: WebReq = null;
42: }
43:
44: // Oh, well we tried.
45: return false;
46: }
47: }