Home > Mobile >  Put folders in an array c#
Put folders in an array c#

Time:11-21

How do I build a console program that will read folders and files from a root folder and be able to output each folder name and the filenames. So my path would be, C:\base\dirA\dirB\dirC\filename.txt.

I want to output dir-A, dir-B, dir_C and filename.txt. I want to avoid using \ as a delimiter or picking out the pattern using regex. Can you get the folder list into an array?

CodePudding user response:

iterative approach with while loop

System.IO.FileInfo fi = new System.IO.FileInfo(@"C:\base\dirA\dirB\dirC\filename.txt");
Console.WriteLine(fi.Name);
System.IO.DirectoryInfo di = fi.Directory;
while (di.Name != di.Root.Name)
{
    Console.WriteLine(di.Name);
    di = di.Parent;
}
  •  Tags:  
  • c#
  • Related