91网首页-91网页版-91网在线观看-91网站免费观看-91网站永久视频-91网站在线播放

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

C#判斷網絡鏈接狀態以及遠程是否存在某個文件

admin
2021年1月30日 9:16 本文熱度 4133
方法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-2025 ClickSun All Rights Reserved

主站蜘蛛池模板: 97精品在线| 韩国男女 | 无码av岛国片在线观看网站 | 国产重口 | 91福利站| 精品欧美А∨被黑人 | 日本成年网 | 91制片一二三 | 强伦中文字幕在线 | 91热国产在线观看 | 午夜国产福利看片 | 欧美日韩在线成人 | 飘花电影网 | 人人干美女| 国产极品喷 | 国产视频三级 | 最新日韩午夜在线电影 | www.色日本 | 吊钟乳在线91 | 欧美在线观看一区 | 精品国产免费1区 | 欧美日韩亚州在线 | 午夜影视九九国产 | 成人自拍偷拍视频 | 欧洲成人在| 福利影院在线播放 | 强伦中文字幕在线 | 国产传媒欧 | 国产色综合 | 午夜日韩绝美 | 国产玉足免费观看 | 国产高清在线自在 | 91极品看片| 国产中文乱伦三级 | 成人午夜羞羞网站 | 国产毛多水多女人a | 午夜拍拍拍 | 国产不卡在线二区 | 国产又色又爽又 | 国产精品亚洲国产在 | 伦理片在线观看伦 |