简体   繁体   English

如何在一个文件夹中枚举以获取ASP.NET MVC网站中的所有文件名?

[英]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: 换一种说法:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM