如题,文件夹内有不同类型文件,需要把一个文件夹内的图片文件在 DataList 里输出
原先是遍历文件夹内所有文件,代码如下,可是只需要输出图片文件DirectoryInfo dir = new DirectoryInfo(Server.MapPath("/UPFile/news/" + txtid.Text));
FileSystemInfo[] dis = dir.GetFileSystemInfos();
if (dis.Length >= 1)
{
DataList1.DataSource = dis;
DataList1.DataBind();
}后来想了个办法过滤类型输出这些图片,可是因为需要添加删除图片按钮我希望能把这些写进 DataSource 去绑定 DataList int h=1;
int j = 0;
txtgndq.Text = "";
DirectoryInfo dir = new DirectoryInfo(Server.MapPath("/images/icon"));
foreach (FileInfo dChild in dir.GetFiles("*"))
{ int i = dChild.Name.LastIndexOf(".");
string Fname = dChild.Name.Substring(0, i);
string newext = dChild.Name.Substring(i); //获取文件扩展名
if (newext == ".gif" || newext == ".jpg" || newext == ".jpeg" || newext == ".bmp" || newext == ".png")
{
txtgndq.Text += "<img src='/images/icon/" + dChild.Name + "'/> ";
if ((h % 13) == 0) txtgndq.Text += "<br>";
h++;
}
}求高手指点
原先是遍历文件夹内所有文件,代码如下,可是只需要输出图片文件DirectoryInfo dir = new DirectoryInfo(Server.MapPath("/UPFile/news/" + txtid.Text));
FileSystemInfo[] dis = dir.GetFileSystemInfos();
if (dis.Length >= 1)
{
DataList1.DataSource = dis;
DataList1.DataBind();
}后来想了个办法过滤类型输出这些图片,可是因为需要添加删除图片按钮我希望能把这些写进 DataSource 去绑定 DataList int h=1;
int j = 0;
txtgndq.Text = "";
DirectoryInfo dir = new DirectoryInfo(Server.MapPath("/images/icon"));
foreach (FileInfo dChild in dir.GetFiles("*"))
{ int i = dChild.Name.LastIndexOf(".");
string Fname = dChild.Name.Substring(0, i);
string newext = dChild.Name.Substring(i); //获取文件扩展名
if (newext == ".gif" || newext == ".jpg" || newext == ".jpeg" || newext == ".bmp" || newext == ".png")
{
txtgndq.Text += "<img src='/images/icon/" + dChild.Name + "'/> ";
if ((h % 13) == 0) txtgndq.Text += "<br>";
h++;
}
}求高手指点
以下是代码: int h = 1;
int j = 0;
txtgndq.Text = "";
DirectoryInfo dir = new DirectoryInfo(Server.MapPath("/images"));
List<FileSystemInfo> list = dir.GetFileSystemInfos().ToList<FileSystemInfo>();
foreach (FileInfo dChild in dir.GetFiles("*"))
{ int i = dChild.Name.LastIndexOf(".");
string Fname = dChild.Name.Substring(0, i);
string newext = dChild.Name.Substring(i); //获取文件扩展名
if (newext == ".gif" || newext == ".jpg" || newext == ".jpeg" || newext == ".bmp" || newext == ".png")
{
txtgndq.Text += "<img src='/images/" + dChild.Name + "'/> ";
if ((h % 13) == 0) txtgndq.Text += "<br>";
h++; }
else
{
list.Remove (list.Find(p=>p.Name==dChild.Name));
}
}
DataList1.DataSource = list;
DataList1.DataBind();
DataList1.DataSource = imagesfile.GetFiles("*.jpg");
DataList1.DataBind();
或定义实体类,通过遍历文件夹添加对象到
List<T> lst=new List<T>();
再绑定到DataList1