asp.net 判断能否连网 可以 返回1 不可以返回0

laj0600310219 2011-07-05 05:26:02
谢谢
...全文
90 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
水中锋 2011-07-10
  • 打赏
  • 举报
回复
学习下
laj0600310219 2011-07-06
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 porschev 的回复:]
这个方法其实也行。。。ping一下百度。。。
[/Quote]谢谢 用的你的方法 测试通过
nfclass 2011-07-05
  • 打赏
  • 举报
回复
200分哦 不是有限制只能100分木
燃烧土豆 2011-07-05
  • 打赏
  • 举报
回复
还有一个比较简单的方法,只要一句话,一时之间想不起来了....
燃烧土豆 2011-07-05
  • 打赏
  • 举报
回复

private string CheckNetWork(string _strUrl)
{
//实例一个Process类,启动一个独立进程
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("telnet -n 1 " + _strIP + " 84564646");
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
//strRst = p.StandardError.ReadToEnd();
//p.WaitForExit();
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 = "网络无效";
p.Close();
return PingRst;

}
q107770540 2011-07-05
  • 打赏
  • 举报
回复

process通过cmd ping
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
public bool IsInternetConnected()
{
int i = 0;
return InternetGetConnectedState(out i, 0);
}
子夜__ 2011-07-05
  • 打赏
  • 举报
回复
wininet
[DllImport("wininet")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
///<summary>
/// 检测本机的网络连接
///</summary>

private void button1_Click(object sender, EventArgs e)
{
//判断是否联网
int i = 0;
if (InternetGetConnectedState(out i, 0))
{
//联网
MessageBox.Show("Thylx提醒您:你的计算机已连接到网络上!");
}
else
{
//断网
MessageBox.Show("Thylx提醒您:本地连接已断开!");
}
}

暖枫无敌 2011-07-05
  • 打赏
  • 举报
回复
zmm12 2011-07-05
  • 打赏
  • 举报
回复

try
{
System.Net.WebClient wc =new System.Net.WebClient();
string bb=wc.DownloadString("http://www.baidu.com");
}
catch(Exception ex)
{
//没有联网
return "我就不返回0";
}

62,051

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧