I have created a images folder inside my wwwroot. The Images folder also have subfolders. I want to get these subfolders so I can send them into view using ViewBag but I don't know how to reach them.
private string mainDirectory; public TestController(IWebHostEnvironment hostingEnvironment) { _hostingEnvironment = hostingEnvironment; mainDirectory = Path.Combine(Directory.GetCurrentDirectory(), _hostingEnvironment.WebRootPath, "images"); string[] files = Directory.GetFiles(mainDirectory) .Select(f => Path.GetFileName(f)) .ToArray(); } I managed to get the files inside images folder with the code above (string[] files) but I need subfolders not files.
Directory.GetFile, although the formatting is not optimal. Did you check the docs?EnumerateFilesif you intend to use LINQ. You can passSearchOptions.AllDirectoriesto scan recursively. You can useEnumerateDirectoriesto returne directories only