In my MVC application I have the following paths;
How would I, in my c# controller, get a list of all the files within my thumbs folder?
Edit
Is Server.MapPath still the best way?
I have this now DirectoryInfo di = new DirectoryInfo(Server.MapPath("/content/images/thumbs") );
but feel it's not the right way.
is there a best practice in MVC for this or is the above still correct?
You can use the DIR command by itself (just type “dir” at the Command Prompt) to list the files and folders in the current directory.
Use the listdir() and isfile() functions of an os module to list all files of a directory.
.NET 4.0 has got a more efficient method for this:
Directory.EnumerateFiles(Server.MapPath("~/Content/images/thumbs"));
You get an IEnumerable<string>
on which you can iterate on the view:
@model IEnumerable<string> <ul> @foreach (var fullPath in Model) { var fileName = Path.GetFileName(fullPath); <li>@fileName</li> } </ul>
Directory.GetFiles("/content/images/thumbs")
That will get all the files in a directory into a string array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With