C# 获取指定类型的文件

C# 获取指定类型的文件

  

  public static List<FileInfo> getFile(string path, string extName)
  {
    List<FileInfo> lst = new List<FileInfo>();

    try
    {
      string[] dir = Directory.GetDirectories(path); //文件夹列表
      DirectoryInfo fdir = new DirectoryInfo(path);
      FileInfo[] file = fdir.GetFiles();
      //FileInfo[] file = Directory.GetFiles(path); //文件列表
      if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空
      {
        foreach (FileInfo f in file) //显示当前目录所有文件
        {
          if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
        {
        lst.Add(f);
      }
    }
        foreach (string d in dir)
        {
          getFile(d, extName);//递归
        }
     }
    return lst;
    }catch (Exception ex){

      return lst;
    }
   }

posted @ 2019-09-12 15:07  蜗牛的密密  阅读(2623)  评论(0编辑  收藏  举报