如何得知当前计算机的网络是连通还是断开?是否只能用定时器?有没有别的更好的方法?

解决方案 »

  1.   

    using System.Runtime.InteropServices;
    [DllImport("wininet.dll")]        private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
    private static bool IsConnected()
            {            int I = 0;            bool state = InternetGetConnectedState(out I, 0);            return state;        }        static void Main(string[] args)
            {
                Console.WriteLine(IsConnected().ToString());
            }
      

  2.   

    也可以调用cmd,使用ping命令来测试
      

  3.   

    嗯 我的意思是像MSN一样 如果网络断了 就立刻弹出对话框 提示断开 如果网络连上了 也会立刻作出反应
      

  4.   

    嗯 谢谢 但是 如果网络突然断开(比如网线掉了)那我的程序如何得到通知呢? 是不是得用定时器? 有没有更好的办法?---------------------
    就是加个timer不停的去判断是否连接,也可调用CMD,使用PING命令不停的拼它