How to get the names of a subfolder in c#

I have a folder path with me something like "c:/videos". it contains subfolders like car, bike, bus … etc. need to get only the sub folder name and store in a string array.

And please note i don’t need a full sub folder path

out needed like:- car, bike, bus

not like c:/videos/car

>Solution :

You have to iterate over the SubDirectories.
And replace the startpath c:\videoswith an empty string:

var rootDir = @"c:\videos";
DirectoryInfo directoryInfo = new DirectoryInfo(rootDir);

var dirs = new System.Collections.Generic.List<string>();
foreach (var dir in directoryInfo.GetDirectories())
    dirs.Add(dir.Name.Replace($"{rootDir}\\", ""));
var result = dirs.ToArray();

enter image description here

Leave a Reply