C#判斷網(wǎng)絡(luò)鏈接狀態(tài)以及遠(yuǎn)程是否存在某個文件
當(dāng)前位置:點晴教程→知識管理交流
→『 技術(shù)文檔交流 』
方法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
[ ]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
//以及判斷網(wǎng)絡(luò)連接的函數(shù):
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 = "狀態(tài):網(wǎng)絡(luò)暢通!";
}
else
{
Label1.Text = "狀態(tài):與目標(biāo)網(wǎng)絡(luò)無連接!";
}
} 方法3: //這個方法有時不是那么好用,因為禁止ping域名是某些服務(wù)商防范黑客攻擊的一種手段
/// <summary>
/// ping 具體的網(wǎng)址看能否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;
} 判斷遠(yuǎn)程有無此文件方法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;
} 判斷遠(yuǎn)程有無此文件方法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#關(guān)于判斷網(wǎng)絡(luò)連接正常與否的總結(jié) 本人最近做c 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網(wǎng)易 string ip = this.txtip.Text.ToString(); int port = Convert .ToInt32( this.txtport.Text.ToString()); bool a = panduan(ip, port );//135為本機服務(wù)端口號 if (a == true) { MessageBox.Show("該網(wǎng)絡(luò)連接正常 !"); } else { MessageBox.Show("該網(wǎng)絡(luò)連接不暢通 !"); } } // 異步調(diào)用 //判斷的方法 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 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; //檢查網(wǎng)絡(luò)連接 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("連接失?。?/span>", "提示信息"); } } 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 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |