仅在asp.net中显示所需的文件

时间:2011-05-24 06:43:33

标签: c# asp.net file

  DirectoryInfo Dir = new DirectoryInfo(Server.MapPath(strheadlinesid));
  FileInfo[] FileList = Dir.GetFiles("*.txt", SearchOption.AllDirectories);

在* .txt的地方,我想提一些文件扩展名,我该怎么做。

我使用了另一种方法,但是当我使用FI作为超链接时,我有一个小问题,它给出了总路径。但我想只打印文件名而不是完整路径。

   string supportedExtensions = "*.jpg,*.gif,*.png,*.bmp,*.jpe,*.jpeg,*.wmf,*.emf,*.xbm,*.ico,*.eps,*.tif,*.tiff,*.g01,*.g02,*.g03,*.g04,*.g05,*.g06,*.g07,*.g08";

   foreach (string FI in Directory.GetFiles(Server.MapPath(strheadlinesid), "*.*", SearchOption.AllDirectories).Where(s => supportedExtensions.Contains(Path.GetExtension(s).ToLower())))
   {

       Response.Write("<td><a href= view5.aspx?file=" + strheadlinesid + "\\" + FI + " target=_self;> " +
           FI + "</a></td>");

   }

1 个答案:

答案 0 :(得分:3)

尝试

string fileFilter = "*.wma,*.jpeg,*.txt";
            string[] fileExt = fileFilter.Split(',');
            FileInfo[] fileInfo = null;
            DirectoryInfo dir = new DirectoryInfo("D:\\Test");
            List<FileInfo[]> listFileInfo = new List<FileInfo[]>();

            foreach (string strVar in fileExt)
            {
                fileInfo = dir.GetFiles(strVar, SearchOption.AllDirectories);
                listFileInfo.Add(fileInfo);
            }