Crackerjack
Crackerjack

Reputation: 2154

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?

Upvotes: 1

Views: 897

Answers (5)

Crispy
Crispy

Reputation: 5637

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.

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 416149

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.

Upvotes: 0

Crispy
Crispy

Reputation: 5637

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.

Upvotes: 0

theG
theG

Reputation: 585

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.

Upvotes: 2

Tom Ritter
Tom Ritter

Reputation: 101400

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

Upvotes: 0

Related Questions