日期:2014-05-18  浏览次数:21040 次

如何检测远程路径上的一个.exe文件是否存在
http://pcs2.78v.com/仙剑奇侠传3.exe 例如检测这个文件是否存在

------解决方案--------------------
webclient去down一下,然后捕获错误,看看是否404
------解决方案--------------------
private bool UrlIsExist(String url)
{
System.Uri u = null;
try
{
u = new Uri(url);
}
catch { return false; }
bool isExist = false;
System.Net.HttpWebRequest r = System.Net.HttpWebRequest.Create(u) as System.Net.HttpWebRequest;
r.Method = "HEAD";
try
{
System.Net.HttpWebResponse s = r.GetResponse() as System.Net.HttpWebResponse;
if (s.StatusCode == System.Net.HttpStatusCode.OK)
{
isExist = true;
}
}
catch (System.Net.WebException x)
{
try
{
isExist = ((x.Response as System.Net.HttpWebResponse).StatusCode != System.Net.HttpStatusCode.NotFound);
}
catch { isExist = (x.Status == System.Net.WebExceptionStatus.Success); }
}
return isExist;
}

判断网络地址 Url 是否存在的方法
http://dotnet.aspx.cc/file/Delect-Url-Exists.aspx