How can I enumerate through a folder to get all file names in an ASP.NET MVC website?
How can I see all the images (*.jpg
and *.png
) in a folder, in my website?
All I can give is th开发者_Python百科e location of the folder, but that is relative.
eg. ~/Images/Uploaded
or /Images/Uploaded
.
cheers :)
It sounds like you want:
var files = Directory.GetFiles(Server.MapPath(path));
In other words:
- Use
MapPath
to get from the web path to the file system path - Use
Directory.GetFiles
to find files on the file system
Jon beats me to it but it may be worth mentioning the overload that takes a search pattern:
string[] files = Directory.GetFiles(Server.MapPath(path), "*.png");
You'll need to mix the results for the 2 image types.
Not sure what exactly you want to do BUT you need to first resolve the relative path to a "real one" using HttpContext.Current.Server.MapPath
- for an example see http://www.dotnetperls.com/mappath
精彩评论