using System.Net.NetworkInformation;
bool isLocalAreaConnected = NetworkInterface.GetIsNetworkAvailable();
if (isLocalAreaConnected)
{
Common.MessageBox.ShowMes(this,"有链接");
}
using System.IO;
using System.Runtime.InteropServices;//引入这两个命名空间,不用引用wininet.dll [DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
//以及判断网络连接的函数:
private bool IsConnected()
{ int I = ; bool state = InternetGetConnectedState(out I, ); return state; } protected void Button1_Click(object sender, EventArgs e)
{
if (IsConnected())
{
Label1.Text = "状态:网络畅通!";
}
else
{
Label1.Text = "状态:与目标网络无连接!";
}
}
//这个方法有时不是那么好用,因为禁止ping域名是某些服务商防范黑客攻击的一种手段
/// <summary>
/// ping 具体的网址看能否ping通
/// </summary>
/// <param name="strNetAdd"></param>
/// <returns></returns>
private static bool PingNetAddress(string strNetAdd)
{
bool Flage = false;
Ping ping = new Ping();
try
{
PingReply pr = ping.Send(strNetAdd, );
if (pr.Status == IPStatus.TimedOut)
{
Flage = false;
}
if (pr.Status == IPStatus.Success)
{
Flage = true;
}
else
{
Flage = false;
}
}
catch
{
Flage = false;
}
return Flage;
}
判断远程有无此文件

using System.Net;
private bool RemoteFileExists(string fileUrl)
{
try
{
HttpWebRequest re = (HttpWebRequest)WebRequest.Create(fileUrl);
HttpWebResponse res = (HttpWebResponse)re.GetResponse();
if (res.ContentLength != )
{
return true;
//MessageBox.Show("文件存在"); return true;
}
}
catch (Exception)
{
//Response.Write("不存在");
return false;
}
return false;
}
判断远程有无此文件
using System.Net;
public static bool IsExist(string uri)
{
HttpWebRequest req = null;
HttpWebResponse res = null;
try
{
req = (HttpWebRequest)WebRequest.Create(uri);
req.Method = "HEAD";
req.Timeout = ;
res = (HttpWebResponse)req.GetResponse();
return (res.StatusCode == HttpStatusCode.OK);
}
catch
{
return false;
}
finally
{
if (res != null)
{
res.Close();
res = null;
}
if (req != null)
{
req.Abort();
req = null;
}
}
}
//2:
private bool UrlExistsUsingXmlHttp(string url)
{
//注意:此方法需要引用Msxml2.dll( 项目---添加引用--com---microsoft xml 2.6)
MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass();
_xmlhttp.open("HEAD", url, false, null, null);
_xmlhttp.send("");
return (_xmlhttp.status == );
}
//3:
private bool UrlExistsUsingSockets(string url)
{
if (url.StartsWith("http://")) url = url.Remove(, "http://".Length);
try
{
System.Net.IPHostEntry ipHost = System.Net.Dns.GetHostEntry(url);// System.Net.Dns.Resolve(url);
return true;
}
catch (System.Net.Sockets.SocketException se)
{
System.Diagnostics.Trace.Write(se.Message);
return false;
}
}

c#关于判断网络连接正常与否的总结

本人最近做c#winform的项目,遇到了判断网络是否正常连接的问题。后来查出了以下几种方法,供大家学习参考。
.方法一
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Demo : Form
{
public Demo()
{
InitializeComponent();
}
//判断
private void btpanduan_Click(object sender, EventArgs e)
{
//210.192.120.228 163网易
string ip = this.txtip.Text.ToString();
int port = Convert .ToInt32( this.txtport.Text.ToString());
bool a = panduan(ip, port );//135为本机服务端口号
if (a == true)
{
MessageBox.Show("该网络连接正常 !");
}
else
{
MessageBox.Show("该网络连接不畅通 !");
}
} // 异步调用 //判断的方法
public bool panduan(string ip, int port)
{
try
{
TcpClient client = new TcpClient(ip, port);
if (client.Connected)
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
}
} .利用 c# ping类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace WindowsFormsApplication1
{
public partial class Demo3 : Form
{
public Demo3()
{
InitializeComponent();
}
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply res;
//检查网络连接
private void btcheck_Click(object sender, EventArgs e)
{
string url = this.txturl.Text.ToString();
bool a = check(url);
if (a == true)
{
MessageBox.Show("连接成功!", "提示信息");
}
else
{
MessageBox.Show("连接失败!", "提示信息");
}
}
public bool check(string url)
{
try
{
res = ping.Send(url);
if (res.Status == System.Net.NetworkInformation.IPStatus.Success)
{
return true;
}
else
{
return false;
}
}
catch {
return false;
}
}
}
} 摘自 wanglei_smartfish的专栏

C#判断网络链接状态的更多相关文章

  1. Win8 app判断网络连接状态

    Win8 app判断网络连接状态 NetworkInformation.NetworkStatusChanged += NetworkInformation_NetworkStatusChanged; ...

  2. c#判断网络连接状态示例代码

    使用c#判断网络连接状态的代码. 代码: public partial class Form1 : Form { [DllImport() == true) { label1.Text = " ...

  3. iOS 判断网络连接状态的几种方法

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #801b80 } p.p2 ...

  4. iOSReachability判断网络连接状态

    // //  NetStateManage.h // //  Created by miniu on 15/11/24. //  Copyright © 2015年 mini. All rights ...

  5. 【Android进阶】判断网络连接状态并自动界面跳转

    用于判断软件打开时的网络连接状态,若无网络连接,提醒用户跳转到设置界面 /** * 设置在onStart()方法里面,可以在界面每次获得焦点的时候都进行检测 */ @Override protecte ...

  6. iOS开发——Reachability和AFNetworking判断网络连接状态

    一.Reachability // 监听网络状态改变的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selec ...

  7. android判断网络连接状态、联网类型、运营商

    /** * 获取上网方式 * * @param mContext * @return */ public static String getNetType(Context mContext) { St ...

  8. iOS 网络与多线程--1.检测网络链接状态

    通过Reachability库,检测设备的网络连接状况. 使用到的类库:Reachability Reachability库,是一个iOS环境下,检测设备网络状态的库,可以在网络上搜索下载. 使用之前 ...

  9. Android判断网络连接状态

    有的时候我们的应用可能需要判断当前设备是否联网 private void init() { /** 获得系统级联网管理员对象 */ ConnectivityManager manager = (Con ...

随机推荐

  1. disabled_button 按钮按不下去

    X老师今天上课讲了前端知识,然后给了大家一个不能按的按钮,小宁惊奇地发现这个按钮按不下去,到底怎么才能按下去 检查元素 删除 按钮就可以摁了 出现答案

  2. [转帖]功耗降50%,性能升35%!三星3nm GAA 2021年量产

    功耗降50%,性能升35%!三星3nm GAA 2021年量产 http://www.chinaflashmarket.com/Instructor 在三星晶圆代工技术论坛(Samsung Found ...

  3. soot学习历程---(1)

    今天看了soot生成手册的部分内容,简单摘录一下 Scene 表示分析所处的完整环境,可以借此设置application class(用sootclass),主类(main),point-to和cal ...

  4. 【7.24校内交流赛】T3【qbxt】复读警告

    数据范围:N,key<=1000; 首先看题目背景,显然不是DP就是图论,但是这显然不是个图论,因此这就是个DP: 接下来考虑怎么DP 我们定义dp[i][j]表示现在dp到了第i个数,当前i个 ...

  5. [LeetCode] 39. 组合总和

    题目链接 : https://leetcode-cn.com/problems/combination-sum/ 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ...

  6. HNUSTOJ-1437 无题

    1437: 无题 时间限制: 1 Sec  内存限制: 128 MB提交: 268  解决: 45[提交][状态][讨论版] 题目描述 tc在玩一个很无聊的游戏:每一次电脑都会给一个长度不超过10^5 ...

  7. PCIeのType0与Type1型配置请求与BAR(基地址寄存器)

    PCIe中存在两种配置空间Type0&type1,TYPE0对应非桥设备(Endpoint),Type1对应桥设备(Root和Switch端口中的P2P桥)因为Root每个端口总都含有一个P2 ...

  8. Spark-Streaming获取kafka数据的两种方式:Receiver与Direct的方式

    简单理解为:Receiver方式是通过zookeeper来连接kafka队列,Direct方式是直接连接到kafka的节点上获取数据 Receiver 使用Kafka的高层次Consumer API来 ...

  9. Scala学习笔记(6)对象

    1.单例对象.Scala没有静态方法或字段,可以使用object这个语法结构来达到同样的目的.对象定义了单个实例,包含了你想要的特性. object Accounts{ def newUniqueNu ...

  10. ioncube扩展的安装详细图文教程,适合所有新手

    有些程序在php环境下运行需要安装ionCube Loader的扩展支持,这里跳过介绍这些东西直接说一下怎么让你的主机环境支持这种扩展,顺利的让你的程序运行起来!这个教程适合用云服务器或者独立服务器的 ...