![](/img/trans.png)
[英]How to display all images in a directory folder from database? Using entity framework lambda
[英]How to display all the files from a directory (folder)
我想在listbox
显示所有图片名称。 我试图弄清楚如何检索文件名。 我试过了
protected void Page_Load(object sender, EventArgs e)
{
string u = Request.Url.AbsoluteUri.ToString();
string serverPath = u.Substring(0, u.LastIndexOf("/")) + "/UBOimages";
Label1.Text = serverPath;
DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath("~/UBOimages"));
//FileInfo[] fileInfo = dirInfo.GetFiles();
Label2.Text = dirInfo.ToString();
}
但是标签中的结果如下所示:
http://localhost:49170/UBOimages
C:\Users\John\Documents\UBO\uboWebCustomer\HuronWood\HuronWood\UBOimages
并且当加载到服务器上时,此页面将显示错误,因为它不喜欢dirInfo路径。 从文件夹(例如图像文件夹)检索所有文件的正确方法是什么
DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath("~/UBOimages"));
FileInfo[] fileInfo = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);
循环浏览此fileinfo数组或将其绑定到任何Gridview或listview。
如果您需要获取名为“ Data”的文件夹中的所有文件,只需将其编码如下
string[] Documents = System.IO.Directory.GetFiles("../../Data/");
现在,“文档”由文件夹“数据”中文件的完整对象名称组成。
System.IO.DirectoryInfo.GetFiles
将帮助您获取给定目录的所有文件。
DirectoryInfo.ToString
不能用于列出目录文件。 它将给出当前DirectoryInfo
对象的字符串表示形式,您会看到在这种情况下它是目录路径。
@我正在使用以上答案根据您的问题编写我的代码!!!
Using System.IO
protected void ListFiles()
{
const string MY_DIRECTORY = "/MyDirectory/";
string strFile = null;
foreach (string s in Directory.GetFiles(Server.MapPath(MY_DIRECTORY), "*.*")) {
strFile = s.Substring(s.LastIndexOf("\\") + 1);
ListBox1.Items.Add(strFile);
}
希望这可以帮助!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.