我在网上看到很多方法
        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue); 
   
      
        private void button1_Click(object sender, EventArgs e)
        {
            int I = 0;
            bool check = InternetGetConnectedState(out I, 0);            if (check == true)
            {
                MessageBox.Show("连上internet");
            }
            else if (check == false)
            {
                MessageBox.Show("没连上");
            }        }我发觉这个方法只能判断机子有无连上局域网。。那判断有无连上internet应该怎样做呢??

解决方案 »

  1.   

    ping的通 www.baidu.com
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
      

  2.   

         //检测网络连接是否连接到Internet
         [DllImport("wininet.dll")]
         private extern static bool InternetGetConnectedState(out int  connectionDescription, int reservedValue);
         public bool IsConnected()
         {
            int connectionDescription = 0;
            //判断是否连接到外网上的函数,并返回布尔值
            return InternetGetConnectedState(out connectionDescription,  0);
         }
      

  3.   

    BOOL InternetGetConnectedStateEx(
      __out  LPDWORD lpdwFlags,
      __out  LPTSTR lpszConnectionName,
      __in   DWORD dwNameLen,
      __in   DWORD dwReserved
    );
      

  4.   


     private void button1_Click(object sender, EventArgs e)
            {
                bool flag = IsConnected();
                MessageBox.Show(flag.ToString());
            }        [DllImport("wininet.dll")]
            private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);        public bool IsConnected()
            {
                int connectionDescription = 0;
                //判断是否连接到外网上的函数,并返回布尔值
                return InternetGetConnectedState(out connectionDescription, 0);
            }我测试了可以的啊
      

  5.   


    你插着网线,然后把IP乱改(这样就连不上internet了),然后你再运行一下,它会提示true的。
      

  6.   

    我试了可以啊,把直接断网后就是false了
    InternetGetConnectedState这个就是判断是否连接Internet的API函数,方法肯定没错的
      

  7.   

    更详细的方法,搂主可以看看下面网页:
    http://www.zdwork.cn/content/20084/68.htm
      

  8.   

    InternetGetConnectedState我也试过,没用的,只要网线插了,本地连接是连接状态,它都返回true!
      

  9.   


    也是InternetGetConnectedState这个方法啊。。
    55555555.。UP!~
      

  10.   


    InternetGetConnectedState,用中文翻译一下,确实是这个。。但真的不行哦,只要网线是插的,它就会true。。会不会是差了些什么步骤呢?
      

  11.   

    最绝的办法不如搂主直接Ping 百度或google之类的网站.net下使用System.Net.NetworkInformation.Ping的Send方法
     ping.Send("www.google.cn",......)
      

  12.   


    Ping.Send (IPAddress, Int32, Byte[], PingOptions) 是这个方法吧?我看了几次没看懂它的意思,可以简单说它的操作吗?这样我会容易一点看明白的。。谢谢你
      

  13.   

    建个webbrowser,navigation到www.google.cn
    查看他的html文档,字符串检测是否包含"Google"
      

  14.   

    帮搂主测试了一把,还行:
    System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
    System.Net.NetworkInformation.PingReply res = ping.Send("www.google.cn");
    if (res.Status != System.Net.NetworkInformation.IPStatus.Success)
    {
        MessageBox.Show("您没有连接到Internet");
    }
    else
    {
        MessageBox.Show("您已经连接到Internet");
    }
      

  15.   

    注意处理send方法的异常,他有很多异常
    有空再帮搂主找找其他办法,因为不太推荐这种做法
      

  16.   


    OH。。YEAH、、马上研究。。谢谢你啊
      

  17.   

    调用:CmdPing(ip);实现:private static string CmdPing(string strIp)
            {
                Process p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;
                string pingrst;
                p.Start();
                p.StandardInput.WriteLine("ping -n 1 " + strIp);
                p.StandardInput.WriteLine("exit");
                string strRst = p.StandardOutput.ReadToEnd();
                if (strRst.IndexOf("(0% loss)") != -1)
                    pingrst = "已连接到系统服务器...";
                else if (strRst.IndexOf("Destination host unreachable.") != -1)
                    pingrst = "无法到达系统服务器...";
                else if (strRst.IndexOf("Request timed out.") != -1)
                    pingrst = "连接系统服务器超时,请检查网络...";
                else if (strRst.IndexOf("Unknown host") != -1)
                    pingrst = "无法解析系统服务器...";
                else
                    pingrst = strRst;
                p.Close();            return pingrst;
            }
      

  18.   

      //检测网络连接是否连接到Internet
         [DllImport("wininet.dll")]
         private extern static bool InternetGetConnectedState(out int  connectionDescription, int reservedValue);
         public bool IsConnected()
         {
            int connectionDescription = 0;
            //判断是否连接到外网上的函数,并返回布尔值
            return InternetGetConnectedState(out connectionDescription,  0);
         }
      

  19.   


            /// <summary>
            /// 判断是否连接到Internet
            /// </summary>
            /// <returns></returns>
            static public bool InternetGetConnectedState()
            {
                try
                {
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.google.com");      
                HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 
                myHttpWebResponse.Close();
                return true;
                }
                catch
                {
                    return false;
                }
            }