-3

I have a folder named A which has subfolders A1, A2 and A3. Each of these subfolders contain a folder named B. I want to seach the subsfolders of A which are A1, A2 and A3 for the B folder and if it exist I want to get from that folder the latest file path.

var directory = new DirectoryInfo(@"C:\app\A\A1\B");

var myFile = directory.GetFiles()
             .OrderByDescending(f => f.LastWriteTime)
             .First();

I wan to avoid using the directory var and search instead.

poke
  • 369,085
  • 72
  • 557
  • 602
Oh hi Mark
  • 153
  • 1
  • 8
  • 28

1 Answers1

0

You can use GetDirectories() method in Directory class with the wildcard character "*". This wildcard will list all the directories under the current directory.

So an example is like this:

string[] dirs = Directory.GetDirectories(@"C:\app\", "*", SearchOption.AllDirectories);
foreach (string dir in dirs)
{
    Response.Write(dir + "<br>");
}
poke
  • 369,085
  • 72
  • 557
  • 602