0

How do we search and obtain all the subdirectories that match name as the serch parameter within a directory. For example, I have a directory in my C drive called UserInfo. There are hundreds of subdirectories (and nested subdirectories. there are more than 6 levels of nesting) in this UserInfo Directory. I wanna obtain all the subdirectories (including nested subdirectories) with names either UserLocal or UserGlobal. My aim is to get all the files inside the folders with names UserGlobal or UserLocal. Is it possible to do in one iteration or do I need two separate searches ?Directory levels-example

static void Main (string[] args)
{
    DirectoryInfo directory = new DirectoryInfo (@"C:\UserInfo");
}


dragon123
  • 303
  • 1
  • 3
  • 13
  • Hello. You might want to start with this: https://stackoverflow.com/questions/929276/how-to-recursively-list-all-the-files-in-a-directory-in-c. Use that as a building block. I tried the code and it works for a very large tree of folders – Radu Caprescu Mar 15 '22 at 10:18
  • @RaduCaprescu but how do we filter subdirectory by names ? – dragon123 Mar 15 '22 at 10:28
  • 1
    Time to learn about the Microsoft Docs Online: [`EnumerateDirectories(String, SearchOption)`](https://learn.microsoft.com/en-us/dotnet/api/system.io.directoryinfo.enumeratedirectories?view=net-6.0#system-io-directoryinfo-enumeratedirectories(system-string-system-io-searchoption)) – Fildor Mar 15 '22 at 10:37
  • *Is it possible to do in one iteration* - yes, though I think not with the pattern facility of the existing enumerate methods because it;s quite limited.. unless you want to search for `User*` and then check if the full string is UserGlobal or UserLocal. If there's nothing common to use then just enumerate everything and use C# case insense string matching to determine if to act – Caius Jard Mar 15 '22 at 10:44

0 Answers0