Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get most recent modification date of all the files in a directory

I am a real newbie in C#, and I am trying to get the most recent modification time (LastWriteTime) for any file in a directory.

What is a simple way to get this information ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I would simplify your code to use this approach:

DirectoryInfo di_source_directory = new DirectoryInfo(@"C:\MyFolder");
FileSystemInfo[] ls_fi = di_source_directory.GetFileSystemInfos();
DateTime ts_most_recent = (ls_fi.Any() 
                 ? ls_fi.Max(fi => fi.LastWriteTime) 
                 : default(DateTime));

or just replace the default(DateTime) part with new DateTime(…..)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading