c# - 如何在 C# 中查询互联网连接状态?

标签 c#

查找用户是否连接到 Internet 的最佳和快速方法是什么?

最佳答案

试试这个:

   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: }

来源:http://blog.dotnetclr.com/archive/2007/09/24/Check-for-internet-connection---Method-2.aspx

关于c# - 如何在 C# 中查询互联网连接状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3802171/

相关文章:

c# - 为什么在创建新对象时 C# 链表中的值会发生变化?

c# - 向右移动数组元素?

c# - Azure 预编译函数的 URL 格式应该是什么?

c# - 在列表框中显示目录中的单词文件列表 (Asp.net c#)

c# - CF 2.0 List<T>.Sort 上的 MethodAccessException

c# - 带有构造函数的通用类<T>来选择类型

c# - AsParallel.ForAll 与 Parallel.ForEach

c# - 如何编写从开始到第一个换行符匹配字符串的正则表达式?

C# 将整个数组移动 1

c# - 是否有与 Python 的 chr 和 ord 等效的 C#?