简体   繁体   中英

Checking when an XML File is uploaded with C#

I manage a website that pulls data in from an XML file, the file is updated every night around midnight. I am trying to write a simple piece of code that says on the website, File Last Updated: something something. After poking around and trying several different solutions, (I tried several variations using FileInfo to no avail) I decided to post here.

I tried Both GetCreationTime and GetLastWriteTime with FileInfo but I keep getting back 12/30/1600 or something similar no where near the correct date. When the file gets uploaded it just overwrites the old one so I wasn't sure which method to use either.

Current Filinfo Code:

    FileInfo fileInfo = new FileInfo("feed/VEHICLES.XML");
    DateTime timeOfCreation = fileInfo.CreationTime;
    modifieddate.Text = timeOfCreation.ToString();

this returns 12/31/1600 7:00:00 PM but it should be 11/14/2011 1:41 AM

The file doesn't exist because the path is bad.

You can check fileInfo.Exists if it returns false then you get 12/31/1600 7:00:00 PM for the timeOfCreation

You're using a relative path without knowing what the current directory is.

You probably want Server.MapPath("~/feed/VEHICLES.XML") .
If the file isn't in your application directory, you'll need to find its absolute path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM