Home > Enterprise >  C# - System.IO - selecting the last updated folder
C# - System.IO - selecting the last updated folder

Time:09-22

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??

CodePudding user response:

You can use this code for get last updated directory:

var directories = new DirectoryInfo("directory_path").GetDirectories().OrderByDescending(t => t.LastWriteTime).ToList();

CodePudding user response:

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);
  • Related