Home > OS >  Find the most recent file saved under same name but in different directories using .NET C#
Find the most recent file saved under same name but in different directories using .NET C#

Time:05-17

The directories are the 3 below

    filename = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    filenameprint = System.Environment.CurrentDirectory;
    pathload = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

And the file is saved as printout.pdf in all the 3 directories.

I'm thinking of using the LastWriteTime function but I do not know how to stack for the latest.

CodePudding user response:

This should do the trick:

    var files = new List<string>() {filename, filenameprint, pathload};
    var newestFile = files.Select(file => new FileInfo(file)).OrderByDescending(f => f.LastWriteTime).First();

Edit: naturally you will have to combine the filename with the paths, otherwise this does not make much sense.

  • Related