简体   繁体   中英

How do you determine when a filename was last changed in C#?

I have small utility that does some processing on a file and changes the file extension to .processed when it is finished. I also want to delete these old .processed files after "x" number of days. Is there a file attribute that tells you when a filename was last changed? I realize that I can add white space to the end of the file after processing, re-save it, and then get the "LastWriteTime" attribute, but I do not want touch the original file at all. Any ideas?

LastWriteTime in the System.IO.FileInfo namespace.

FileInfo fi1 = new FileInfo(path);

Oh yeah, you can write this value as well. Update it when you update the filename.

Another idea if updating the LastWriteTime solution doesn't meet your needs:

Store the filename (or renamed filename) and the date the file was processed in a database.

I don't understand... LastWritetime should be the last time the file was written to. So if your processing modifies the x.processed file, you can look at that. You could also use GetLastAccessTime for the last time it was read.

Edit : Ah, renames don't modify LastWriteTime. Well then set LastWriteTime yourself

I don't think renaming the file updates the LastWriteTime.

So when your process is renaming the file, update the LastWriteTime in the FileInfo object at the same time then you can use the LastWriteTime for your comparison.

Even if windows did update LastWriteTime when you renamed a file, it wouldn't be good enough:

When you first change the name of a file you could check the LastWriteTime date on the new file name, but once you edit the file in any way after that the information is lost.

Fortunately, in this case you will have changed the file in other ways, so it doesn't matter. You can just check it and know that you're okay.

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