LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C#判断网络链接状态以及远程是否存在某个文件

admin
2021年1月30日 9:16 本文热度 2959
方法1:
using System.Net.NetworkInformation; bool isLocalAreaConnected = NetworkInterface.GetIsNetworkAvailable(); if (isLocalAreaConnected) { Common.MessageBox.ShowMes(this,"有链接"); }

方法2:
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 = 0; bool state = InternetGetConnectedState(out I, 0); return state; } protected void Button1_Click(object sender, EventArgs e) { if (IsConnected()) { Label1.Text = "状态:网络畅通!"; } else { Label1.Text = "状态:与目标网络无连接!"; } }

方法3:
//这个方法有时不是那么好用,因为禁止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, 3000); if (pr.Status == IPStatus.TimedOut) { Flage = false; } if (pr.Status == IPStatus.Success) { Flage = true; } else { Flage = false; } } catch { Flage = false; } return Flage; }

判断远程有无此文件方法1:
using System.Net; private bool RemoteFileExists(string fileUrl) { try { HttpWebRequest re = (HttpWebRequest)WebRequest.Create(fileUrl); HttpWebResponse res = (HttpWebResponse)re.GetResponse(); if (res.ContentLength != 0) { return true; //MessageBox.Show("文件存在"); return true; } } catch (Exception) { //Response.Write("不存在"); return false; } return false; }

判断远程有无此文件方法2: 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 = 100; 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 == 200); } //3: private bool UrlExistsUsingSockets(string url) { if (url.StartsWith("http://")) url = url.Remove(0, "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的项目,遇到了判断网络是否正常连接的问题。后来查出了以下几种方法,供大家学习参考。 1.方法一 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; } } } } 2.利用 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; } } } }


该文章在 2021/1/30 9:16:45 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved