Go to home page mail me! RSS Feed

Check for internet connection - Method #2

Monday, September 24, 2007 10:32 AM

 A better and cleaner way for checking for an active internet connection. see http://blog.dotnetclr.com/archive/2007/08/30/Check... for the older version.

   1: /// <summary>
   2: /// Performs actions on the network
   3: /// </summary>
   4: public sealed class NetworkHandler
   5: {
   6:     /// <summary>
   7:     /// Private constructor to prevent compiler from generating one
   8:     /// since this class only holds static methods and properties
   9:     /// </summary>
  10:     NetworkHandler() { }
  11:  
  12:     /// <summary>
  13:     /// SafeNativeMethods Class that holds save native methods 
  14:     /// while suppressing unmanaged code security
  15:     /// </summary>
  16:     [SuppressUnmanagedCodeSecurityAttribute]
  17:     internal static class SafeNativeMethods
  18:     {
  19:         // Extern Library
  20:         // UnManaged code - be careful.
  21:         [DllImport("wininet.dll", CharSet = CharSet.Auto)]
  22:         [return: MarshalAs(UnmanagedType.Bool)]
  23:         private extern static bool 
  24:             InternetGetConnectedState(out int Description, int ReservedValue);
  25:  
  26:         /// <summary>
  27:         /// Determines if there is an active connection on this computer
  28:         /// </summary>
  29:         /// <returns></returns>
  30:         public static bool HasActiveConnection()
  31:         {
  32:             int desc;
  33:             return InternetGetConnectedState(out desc, 0);
  34:         }
  35:     }
  36: }

Your Comments.

  • # C# - check for internet connection

    GravatarYou've been kicked (a good thing) - Trackback from DotNetKicks.com

    Left by DotNetKicks.com at 10/13/2007 4:44 PM

Your Reply.

Comment Form.

Fields denoted with a "*" are required.

You may also like to leave your email or website.

 
Please add 3 and 5 and type the answer here:

Preview Your Comment.

 
Next entries »