首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

判断是否连上internet,该怎么处理

2012-01-18 
判断是否连上internet我在网上看到很多方法[DllImport(wininet.dll)]private extern static bool Intern

判断是否连上internet
我在网上看到很多方法


  [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应该怎样做呢??

[解决办法]
沙发,学习~
[解决办法]
沙发,哎呀板凳,呵呵呵
[解决办法]
ping的通 www.baidu.com
  
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 

http://feiyun0112.cnblogs.com/
[解决办法]

C# code
     //检测网络连接是否连接到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);     }
[解决办法]
这样不能判断连接到外网吗?
[解决办法]
BOOL InternetGetConnectedStateEx(
__out LPDWORD lpdwFlags,
__out LPTSTR lpszConnectionName,
__in DWORD dwNameLen,
__in DWORD dwReserved
);
[解决办法]
C# code
 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);        }
[解决办法]
我试了可以啊,把直接断网后就是false了
InternetGetConnectedState这个就是判断是否连接Internet的API函数,方法肯定没错的
[解决办法]
学习
[解决办法]
更详细的方法,搂主可以看看下面网页:
http://www.zdwork.cn/content/20084/68.htm
[解决办法]
学习了哈`````````
[解决办法]
学习了
[解决办法]
最绝的办法不如搂主直接Ping 百度或google之类的网站

.net下使用System.Net.NetworkInformation.Ping的Send方法
 ping.Send("www.google.cn",......)
------解决方案--------------------


但是这方法比较另类
[解决办法]
建个webbrowser,navigation到www.google.cn
查看他的html文档,字符串检测是否包含"Google"
[解决办法]
up
[解决办法]
帮搂主测试了一把,还行:
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");
}

[解决办法]
注意处理send方法的异常,他有很多异常
有空再帮搂主找找其他办法,因为不太推荐这种做法
[解决办法]
一起学习。
[解决办法]
学习。
[解决办法]
调用:

C# code
CmdPing(ip);
[解决办法]
mark,学习下
[解决办法]
//检测网络连接是否连接到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);
}

[解决办法]
C# code
        /// <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;            }        } 

热点排行