我有一个方法,下载文件,同时保持它的原始名称/扩展名:
public void downloadFile(string urlAddress, string location)
{
_fileHasher = new HashFile(_controlsRef);
using (var downloadClient = new WebClient())
{
downloadClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(Completed);
downloadClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
WebRequest request = WebRequest.Create(urlAddress);
WebResponse response = request.GetResponse();
string contentDisposition = response.Headers["Content-Disposition"];
const string contentFileNamePortion = "filename=";
Int32 fileNameStartIndex = contentDisposition.IndexOf(contentFileNamePortion, StringComparison.InvariantCulture) + contentFileNamePortion.Length;
Int32 originalFileNameLength = contentDisposition.Length - fileNameStartIndex;
string originalFileName = contentDisposition.Substring(fileNameStartIndex, originalFileNameLength);
Uri URL = new Uri(urlAddress);
location += "\" + originalFileName;
this._location = location;
_downloadStopWatch.Start();
try
{
downloadClient.DownloadFileAsync(URL, location);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
然后我查找了一个方法,只有当硬盘上的文件是旧的文件时才下载文件:
public void DownloadAndReplace(FileSystemInfo sourceFile)
{
var requestFile = (HttpWebRequest)WebRequest.Create("");
requestFile.Method = "HEAD";
var responseFile = (HttpWebResponse)requestFile.GetResponse();
if (responseFile.LastModified > sourceFile.LastWriteTime)
{
// downloadFile(Here's the problem);
}
}
我的问题,我如何正确地调用DownloadAndReplace方法的downloadFile方法?
你试过了吗
WebRequest。RequestUri -当在子类中重写时,获取与请求关联的Internet资源的URI。
或HttpWebResponse。ResponseUri