-1

i have a requirement to get the last created directory in a path with respect to its name, as an application i use created 2 directories during the runtime and i need to consider only one of them, which in order appears later. For e.g the app creates 2 folders with name: 60000c and b3c143 and i need the one with the name "b3c143". How can i acheive this? Thanks. I get the latest one using the code:

    string path = @"C:\temp";
        string mostRecentlyModified = Directory.GetDirectories(path)
           .Select(f => new FileInfo(f))
           .OrderByDescending(fi => fi.LastAccessTime)
           .First()
           .FullName;
sina123
  • 159
  • 1
  • 10

1 Answers1

1

You can filter your desired directory with its name by passing it to .Where() func and by its last write time.

string mostRecentlyModified = Directory.GetDirectories(path)
               .Select(d => new DirectoryInfo(d))
               .Where(d => d.Name == "b3c143" && d.Exists) //.Where(d => d.Name.Trim().ToLower().Contains("b3c143".Trim().ToLower()) && d.Exists)
               .OrderByDescending(d => d.LastWriteTime)
               .First()
               .FullName;
er-sho
  • 8,871
  • 2
  • 10
  • 23