下面这段代码的功能是,遍历服务器某文件夹下的所有图片,返回其名称。
DirectoryInfo Dir=new DirectoryInfo(DirPath);
try
{
foreach(FileInfo f in Dir.GetFiles("*.jpg")) //查找文件
{
Response.Write(f.ToString()+"
");
}
}
catch(Exception e)
{
Response.Write(e.Message);
}
DirPath指的是路径,比如:Server.MapPath("Img/jpg")
知识拓展:遍历子文件夹名称的代码
DirectoryInfo Dir = new DirectoryInfo(DirPath);
try
{
foreach(DirectoryInfo d in Dir.GetDirectories())//查找子目录
{
GetDirName(Dir+"\\"+d.ToString());
Response.Write(Dir +"\\"+ d.ToString()+"<br>");
}
}
catch (Exception e)
{
Response.Write(e.Message+"<br>");
}