Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Put folders in an array c#

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading