Check last modified date of file in C#

前端 未结 4 1984
走了就别回头了
走了就别回头了 2020-12-01 11:24

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.

4条回答
  •  青春惊慌失措
    2020-12-01 12:03

    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.

提交回复
热议问题