[英]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? 如何查看网站中文件夹中的所有图像(
*.jpg
和*.png
)?
All I can give is the location of the folder, but that is relative. 我所能提供的只是文件夹的位置,但这是相对的。
eg. 例如。
~/Images/Uploaded
or /Images/Uploaded
. ~/Images/Uploaded
或/Images/Uploaded
。
cheers :) 欢呼:)
It sounds like you want: 听起来像您想要的:
var files = Directory.GetFiles(Server.MapPath(path));
In other words: 换一种说法:
MapPath
to get from the web path to the file system path MapPath
从Web路径获取文件系统路径 Directory.GetFiles
to find files on the file system Directory.GetFiles
在文件系统上查找文件 Jon beats me to it but it may be worth mentioning the overload that takes a search pattern: Jon击败了我,但值得一提的是采用搜索模式的重载:
string[] files = Directory.GetFiles(Server.MapPath(path), "*.png");
You'll need to mix the results for the 2 image types. 您需要混合两种图像类型的结果。
不确定要执行什么操作,但首先需要使用HttpContext.Current.Server.MapPath
将相对路径解析为“真实路径”,有关示例,请参见http://www.dotnetperls.com/mappath
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.