。net判断远程文件是否存在
以下代码能判断文件是否存在,但存在一个问题那就是当网速慢的时候就会出现判断不准确,请各位高手帮忙看一下
private bool GetServerFileExists(string URL)
{
try
{
System.Net.WebRequest s = System.Net.WebRequest.Create(URL);
s.Timeout = 16000;
System.Net.WebResponse a = s.GetResponse();
a.Close();
s.Abort();
return true;
}
catch
{
return false;
}
}
以下代码能判断文件是否存在,但存在一个问题那就是当网速慢的时候就会出现判断不准确,请各位高手帮忙看一下
private bool GetServerFileExists(string URL)
{
try
{
System.Net.WebRequest s = System.Net.WebRequest.Create(URL);
s.Timeout = 16000;
System.Net.WebResponse a = s.GetResponse();
a.Close();
s.Abort();
return true;
}
catch
{
return false;
}
}
解决方案 »
- “参数无效”的问题
- 【急】.net链接MySql数据库,运行一会儿后总是出现Timeout expired.错误,我无奈了,大家帮帮忙,谢谢拉!!!
- 高手进,问个ihttphandler的问题
- ADO.NET Entity Data Model 连接字符串密码?
- 刚刚Linq入门,开发了迷你BBS系统,大家PP
- 关于内查询之后在程序里如何区分的问题 在线急等~~~~~
- 在“ASP.NET WEB应用程序”工具箱里没有树型控件呀,怎么加入?
- 求一正则表达式
- 程序产生的按纽如何添加click事件?
- 我刚装了.net,但是不能新建文件 我没有看到这方面的贴子 求救!!!!
- 求助,CS1012: 字符文本中的字符太多
- 未能加载文件或程序集System.Web.Abstractions,求助高手帮忙~~!
catch (WebException webExcp)
{
去判断是什么异常。
}Each WebException includes a Status property that contains a value from the WebExceptionStatus enumeration. You can examine the Status property to determine the error that occurred and take the proper steps to resolve the error.
WebResponse response = null;
try
{
WebRequest req = WebRequest.Create(fileUrl);
response = req.GetResponse();
result = response == null ? false : true;
}
catch (Exception ex)
{
result = false;
}
finally
{
if (response != null)
{
response.Close();
}
}HttpWebRequest req = (HttpWebRequest)WebRequest.Create("");
req.AllowAutoRedirect = false;
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
if(res.StatusCode == HttpStatusCode.OK)
{ }