//判断服务器上指定路径文件是否存在,如果存在返回false,反之返回true
private bool RemoteFileExists(string fileUrl)
{
bool result = false;//下载结果
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();
}
}
return result;
}
/// <summary>
/// 从服务器下载图片,
/// </summary>
/// <param name="Url">Url地址</param>
/// <param name="Path">保存路径</param>
public void DownImage(string Url, string Path)
{
System.Net.WebClient web = new System.Net.WebClient();
//如果保存路径已经存在同名文件则删除保存路径下的文件
if (File.Exists(Path))
{
File.Delete(Path);
}
//下载
web.DownloadFile(Url, Path);
}
|