0

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.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • That's not MVC code. That's a simple `Directory.GetFile`, although the formatting is not optimal. Did you check the docs? – Panagiotis Kanavos Oct 21 '22 at 10:51
  • You can use `EnumerateFiles` if you intend to use LINQ. You can pass `SearchOptions.AllDirectories` to scan recursively. You can use `EnumerateDirectories` to returne directories only – Panagiotis Kanavos Oct 21 '22 at 10:54

0 Answers0