How to determine properties of a file on a web site

I have a requirement of downloading a file from a web site to the local machine. I do this using the WebClient.DownloadFile method (from within a Windows Form application).

I need to add a check which will determine the modified date of the file on the web site and compare it with the date of the file on the local machine. If both of them do not match, only then download the file.

Does anyone know how to determine the modified date property of a file which is on a web site



Answer this question

How to determine properties of a file on a web site

  • Another_tomcat

    Also, If you are willing to use HttpWebRequest instead of WebClient, then you can take advantage of the HttpWebRequest.IfModifiedSince property.  This tells the server to only send you the new file if it has changed since the date you specify.  The server will send back a 304 status code if the file has not been modified, which lets you know to keep using the version you already have.

    If you don't want to use the underlying HttpWebRequest, then you can actually create a class that inherits from WebClient and just sets the property on the HttpWebRequest when it is created...here is some sample code:



    public class CustomWebClient : WebClient
    {
      protected override WebRequest GetWebRequest(Uri address) {
      WebRequest req = base.GetWebRequest(address);

      if(req is HttpWebRequest)
      {
        
       ((HttpWebRequest)req).IfModifiedSince = //insert your dateTime here;

      }
      return req;
     }
    }

     

     



  • thespectre_2000

    You don't need to explicitly get the modified date and compare. Setting the cache policy to satisfy the request by using the cached copy of the resource if the timestamp is the same as the timestamp of the resource on the server can be done by setting the WebClient.CachePolicy property.

    The example below uses WebRequest to do this:

    public static WebResponse GetResponseFromCache(Uri uri)
    {
    RequestCachePolicy policy =
    new RequestCachePolicy( RequestCacheLevel.Revalidate);
    WebRequest request = WebRequest.Create(uri);
    request.CachePolicy = policy;
    WebResponse response = request.GetResponse();
    Console.WriteLine("Policy level is {0}.", policy.Level.ToString());
    Console.WriteLine("Is the response from the cache {0}", response.IsFromCache);
    return response;

    }

    See the revalidate value on http://msdn2.microsoft.com/en-us/library/system.net.cache.requestcachelevel.aspx



  • How to determine properties of a file on a web site