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.
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.
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.
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.
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; ... }
System.IO.File.GetLastWriteTime is what you need.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With