Using FileInfo to see when a file was updated... on another server

1k Views Asked by At

Hey guys I am pulling in a Vehicle Feed to an autodealer website for one of our clients. Every night at midnight(ish) the new XML file is uploaded to our FTP and it overwrites the current one. Currently he has two Identical websites and the file needs to be uploaded to both, I was looking into setting it up so both websites can use the same XML file so we can cut down on the risk of errors and for convince.

Pulling the file works great, both websites can read the XML file and have no issues displaying the inventory. The issue comes in when I try to display the date the file was last updated. I created a small snippet that reads the date the file was updated and displays "Last Update: and the date" but when I try and reference a non-local file I get a error that says "URI formats are not supported". Does anyone know of a way to do this or if its even possible?

what it currently is

FileInfo fileInfo = new FileInfo(Server.MapPath("~/feed/VEHICLES.XML"));
DateTime timeOfCreation = fileInfo.LastWriteTime;

what i tried

FileInfo fileInfo = new FileInfo("http://www.autodealername.com/feed/VEHICLES.XML");
DateTime timeOfCreation = fileInfo.LastWriteTime;

this was no good

6

There are 6 best solutions below

0
On

FileInfo uses information from the underlying file system which isn't available over HTTP. You'll need to think of some other way.

0
On

if you load the file in this way:

FileInfo fileInfo = new FileInfo("http://www.autodealername.com /feed/VEHICLES.XML");

most likely the file is retrieved to you by IIS or the webserver on that domain/site and this is not the same as opening the file from the file system directly.

I think you have two alternatives at least:

  • open the file from a network share like \\machinename\ShareName\FileName;
  • create a service endpoint on the remote server (WCF or XML web service) which gets in a file name and returns the information you need;
0
On

This can be done via FTP, since you're using it already.

http://msdn.microsoft.com/en-us/library/system.net.ftpwebresponse.lastmodified.aspx

0
On

You can try using a WebRequest using the HEAD method and look for the Last-Modified header.

Here's the code I used...

var web = WebRequest.Create("http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=4") as HttpWebRequest;
web.Method = "HEAD";
var response = web.GetResponse();
var lastModified = DateTime.Parse(response.Headers["last-modified"]);
Console.WriteLine(lastModified);

Here's what the http response looks like (from Fiddler)...

HTTP/1.1 200 OK
Server: nginx/0.8.36
Date: Wed, 23 Nov 2011 17:37:44 GMT
Content-Type: image/png
Connection: keep-alive
Cache-Control: max-age=604800
Last-Modified: Tue, 06 Sep 2011 21:44:29 GMT
ETag: "6237328de6ccc1:0"
Content-Length: 19706
X-Cache: HIT
Accept-Ranges: bytes
0
On

You could also add the updated field to the feed so you can get the last time it was updated from the feed itself.

RSS pubDate: http://www.w3schools.com/rss/rss_tag_pubdate.asp

<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">

<channel>
  <title>W3Schools Home Page</title>
  <link>http://www.w3schools.com</link>
  <description>Free web building tutorials</description>

  <!-- YOU COULD USE THIS -->
  <pubDate>Thu, 27 Apr 2006</pubDate>

  <item>
    <title>RSS Tutorial</title>
    <link>http://www.w3schools.com/rss</link>
    <description>New RSS tutorial on W3Schools</description>
  </item>
</channel>

</rss>

Atom updated: http://www.atomenabled.org/developers/syndication/atom-format-spec.php#rfc.section.1.1

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title>Example Feed</title> 
  <link href="http://example.org/"/>

  <!-- YOU COULD USE THIS -->
  <updated>2003-12-13T18:30:02Z</updated>

  <author> 
    <name>John Doe</name>
  </author> 
  <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>

  <entry>
    <title>Atom-Powered Robots Run Amok</title>
    <link href="http://example.org/2003/12/13/atom03"/>
    <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
    <updated>2003-12-13T18:30:02Z</updated>
    <summary>Some text.</summary>
  </entry>

</feed>
0
On

Maybe try using the FileSystemWatcher Class, which can notify you when a file was changed, modified, etc. Take a look at it.

Good luck!