开发者

How to download a file only when the local file is older

I am trying to compare two files, one on a local computer and another on a web server, if the file on the web server is newer, it is downloaded / overwrites the local one. Although FileInfo will not take URI's, can someone recommend a way around this please

private void checkver()
{
    FileInfo sourceFile = new FileInfo("download.zip");
    if (sourceFile.Exists)
    {
        FileInfo destFile = new FileInfo(@"http://www.google.com/download.zip");
        if (destFile.Exists && destFile.LastWriteTime开发者_开发技巧 >= sourceFile.LastWriteTime)
        {

            MessageBox.Show("File already up to date");

        }
        else
        {
            MessageBox.Show("File is not up to date");
        }
    }
}


Try using HttpWebRequest and HttpWebResponse:

var request = (HttpWebRequest)WebRequest.Create(@"http://www.google.com/download.zip");
request.Method = "HEAD";
var response = (HttpWebResponse)request.GetResponse();

if (response.LastModified > sourceFile.LastWriteTime)
{
    // create another request to download the whole file
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜