用PING的方法我试过了,如果没有得到响应,程序会卡5、6秒左右,实在每办法了 希望大家帮帮忙啊

解决方案 »

  1.   

    可在 PingOptions 里设置 超时时间,不至于每次卡5、6秒的。
    怕卡的话,可以 用个单独线程来 ping 啊。不用ping的话,可参考 NetworkInterface 类
    可检查哪些网络连接可用。
      

  2.   


    Thread op = new Thread(new ThreadStart(YourPing));
    op.IsBackground = true;
    op.Start();
      

  3.   

    第一种方法://导入dll
    [DllImport("wininet.dll", EntryPoint = "InternetGetConnectedState")]
    //判断网络状况的方法,返回值true为连接,false为未连接
    public extern static bool InternetGetConnectedState(out int conState, int reder);
     //在你的button事件中写下如下代码就行
        private void btnNetStatus_Click(object sender, EventArgs e)
            {
                int n =0;
                if (InternetGetConnectedState(out n,0))               {
                         MessageBox.Show("网终处于连接状态");                }
                else               {
                    MessageBox.Show("网络处于未连接状态");               }
            }
    第二种方法: System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
                System.Net.NetworkInformation.PingReply reply = ping.Send("www.baidu.com");
                System.Net.NetworkInformation.IPStatus ipstatus = reply.Status;
    //如果ipstatus为success是连网,否则未连网