我在网上看到很多方法
[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应该怎样做呢??
[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应该怎样做呢??
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
public bool IsConnected()
{
int connectionDescription = 0;
//判断是否连接到外网上的函数,并返回布尔值
return InternetGetConnectedState(out connectionDescription, 0);
}
__out LPDWORD lpdwFlags,
__out LPTSTR lpszConnectionName,
__in DWORD dwNameLen,
__in DWORD dwReserved
);
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);
}我测试了可以的啊
你插着网线,然后把IP乱改(这样就连不上internet了),然后你再运行一下,它会提示true的。
InternetGetConnectedState这个就是判断是否连接Internet的API函数,方法肯定没错的
http://www.zdwork.cn/content/20084/68.htm
也是InternetGetConnectedState这个方法啊。。
55555555.。UP!~
InternetGetConnectedState,用中文翻译一下,确实是这个。。但真的不行哦,只要网线是插的,它就会true。。会不会是差了些什么步骤呢?
ping.Send("www.google.cn",......)
Ping.Send (IPAddress, Int32, Byte[], PingOptions) 是这个方法吧?我看了几次没看懂它的意思,可以简单说它的操作吗?这样我会容易一点看明白的。。谢谢你
查看他的html文档,字符串检测是否包含"Google"
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");
}
有空再帮搂主找找其他办法,因为不太推荐这种做法
OH。。YEAH、、马上研究。。谢谢你啊
{
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;
}
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
public bool IsConnected()
{
int connectionDescription = 0;
//判断是否连接到外网上的函数,并返回布尔值
return InternetGetConnectedState(out connectionDescription, 0);
}
/// <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;
}
}