Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check last modified date of file in C#

I'm looking to find out a way of seeing when a file was last modified in C#. I have full access to the file.

like image 231
Candyfloss Avatar asked Jul 29 '10 07:07

Candyfloss


People also ask

How do I find the last modified date of a file?

The lastModified() method of the File class returns the last modified time of the file/directory represented by the current File object. You can get the last modified time of a particular file using this method.

Which command gives information about time of last modification done on file?

ls command ls – Listing contents of directory, this utility can list the files and directories and can even list all the status information about them including: date and time of modification or access, permissions, size, owner, group etc.

How do you get the last modified date of a file in Unix?

The syntax is pretty simple; just run the stat command followed by the file's name whose last modification date you want to know, as shown in the example below. As you can see, the output shows more information than previous commands.

How do I find the last modified date of a file in CPP?

h> #endif #ifdef WIN32 #define stat _stat #endif auto filename = "/path/to/file"; struct stat result; if(stat(filename. c_str(), &result)==0) { auto mod_time = result. st_mtime; ... }


2 Answers

System.IO.File.GetLastWriteTime is what you need.

like image 79
Dean Harding Avatar answered Oct 29 '22 19:10

Dean Harding


You simply want the File.GetLastWriteTime static method.

Example:

var lastModified = System.IO.File.GetLastWriteTime("C:\foo.bar");  Console.WriteLine(lastModified.ToString("dd/MM/yy HH:mm:ss")); 

Note however that in the rare case the last-modified time is not updated by the system when writing to the file (this can happen intentionally as an optimisation for high-frequency writing, e.g. logging, or as a bug), then this approach will fail, and you will instead need to subscribe to file write notifications from the system, constantly listening.

like image 41
Noldorin Avatar answered Oct 29 '22 21:10

Noldorin