Search code examples
c#system.io.file

C# - System.IO - selecting the last updated folder


I'm trying to write a code where I pick the last updated document from a folder:

ex: InsertProject(document, "C:\Master\959824-5.1.PRO", document.Drawing.Pages.Count - 1);

so in this case the 1 is the revision number and it will change in the next update.

how can I insert the last updated document??


Solution

  • If the file search pattern is fixed then you can find it in the directory by below code,

    var dir =new DirectoryInfo(@"C:\Master"); //Define Master directory info
    var fileFullName =  dir
             .GetFiles("959824-5*.PRO")  //Use given search pattern to find files.
             .OrderByDescending(f => f.CreationTime)  //Sort by Creation time
             .FirstOrDefault()?.FullName ?? string.Empty; //Get full path of the file.
    
    if(!string.IsNullOrEmpty(fileFullName))
           InsertProject(document, fileFullName , document.Drawing.Pages.Count - 1);