Home > DeveloperSection > Forums > C# Read file info from URL
Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    December-02-2014 10:23 PM

 C# C# 
Ratings:


 1 Reply(s)
 1995  View(s)
Rate this:

In my ASP.NET MVC application I'm reading an external file from URL and saving it into a directory on my server. I do this in a loop for every few seconds to have actual data if the file was modified.

 

What I need is to recognize that the LastWrittenTime of the file accessed via the URL is different from file already downloaded to the server. I cannot use FileInfo class because "URI formats are not supported".

 

So how do I get the last written time of the file from the URL without needing to download the full file for every loop?



Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Posted on    December-03-2014 6:51 AM

Assuming your HTTP Server allows this.

 

System.Net.WebRequest req = System.Net.HttpWebRequest.Create("http:\\your\url.ext");

req.Method = "HEAD";

using (System.Net.WebResponse resp = req.GetResponse())

{

    DateTime LastModified;

    if(DateTime.TryParse(resp.Headers.Get("Last-Modified"), out LastModified))

    {

        //Check if date is good and then go to full download method.

    }

}

When this method doesn't work because the server doesn't allow it. Then the only way of doing this is by fully downloading the file.


Don't want to miss updates? Please click the below button!

Follow MindStick