zoukankan      html  css  js  c++  java
  • 如何在Directory.GetFiles()方法中设置多个格式呢?

    什么是语法设置多个文件扩展名的searchPatternDirectory.GetFiles()?例如过滤掉的文件与x和的。ascx扩展。

    // TODO: Set the string 'searchPattern' to only get files with
    // the extension '.aspx' and '.ascx'.
    var filteredFiles = Directory.GetFiles(path, searchPattern);

    LINQ是不是一种选择?它必须是一个searchPattern传入GetFiles,在这个问题中指定。

    var filteredFiles = Directory
     .GetFiles(path, "*.*")
     .Where(file => file.ToLower().EndsWith("aspx") || file.ToLower().EndsWith("ascx"))
     .ToList();


    GetFiles的只能匹配单个模式,但你LINQ调用GetFiles的多模式:

    FileInfo[] fi = new string[]{"*.txt","*.doc"}
     .SelectMany(i => di.GetFiles(i, SearchOption.AllDirectories))
     .Distinct().ToArray();

    我怕你会做财产以后这样,我从这里突变的正则表达式。

    var searchPattern = new Regex(
     @"$(?<=.(aspx|ascx))", 
     RegexOptions.IgnoreCase);
    var files = Directory.GetFiles(path).Where(f => searchPattern.IsMatch(f));

    我会尝试像指定

    var searchPattern = "as?x";

    它应该工作。 

     
    var ext = new string[] { ".ASPX", ".ASCX" };
    FileInfo[] collection = (from fi in new DirectoryInfo(path).GetFiles()
           where ext.Contains(fi.Extension.ToUpper())
           select fi)
           .ToArray();

    编辑:纠正目录和DirectoryInfo之间因不匹配 

    /// <summary>
     /// Returns the names of files in a specified directories that match the specified patterns using LINQ
     /// </summary>
     /// <param name="srcDirs">The directories to seach</param>
     /// <param name="searchPatterns">the list of search patterns</param>
     /// <param name="searchOption"></param>
     /// <returns>The list of files that match the specified pattern</returns>
     public static string[] GetFilesUsingLINQ(string[] srcDirs,
       string[] searchPatterns,
       SearchOption searchOption = SearchOption.AllDirectories)
     {
      var r = from dir in srcDirs
        from searchPattern in searchPatterns
        from f in Directory.GetFiles(dir, searchPattern, searchOption)
        select f;
      return r.ToArray();
     }
    

      

     public static bool CheckFiles(string pathA, string pathB)
     {
      string[] extantionFormat = new string[] { ".war", ".pkg" };
      return CheckFiles(pathA, pathB, extantionFormat);
     }
     public static bool CheckFiles(string pathA, string pathB, string[] extantionFormat)
     {
      System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(pathA);
      System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(pathB);
      // Take a snapshot of the file system. list1/2 will contain only WAR or PKG 
      // files
      // fileInfosA will contain all of files under path directories 
      FileInfo[] fileInfosA = dir1.GetFiles("*.*", 
            System.IO.SearchOption.AllDirectories);
      // list will contain all of files that have ..extantion[] 
      // Run on all extantion in extantion array and compare them by lower case to 
      // the file item extantion ...
      List<System.IO.FileInfo> list1 = (from extItem in extantionFormat
               from fileItem in fileInfosA
               where extItem.ToLower().Equals 
               (fileItem.Extension.ToLower())
               select fileItem).ToList();
      // Take a snapshot of the file system. list1/2 will contain only WAR or 
      // PKG files
      // fileInfosA will contain all of files under path directories 
      FileInfo[] fileInfosB = dir2.GetFiles("*.*", 
              System.IO.SearchOption.AllDirectories);
      // list will contain all of files that have ..extantion[] 
      // Run on all extantion in extantion array and compare them by lower case to 
      // the file item extantion ...
      List<System.IO.FileInfo> list2 = (from extItem in extantionFormat
               from fileItem in fileInfosB
               where extItem.ToLower().Equals   
               (fileItem.Extension.ToLower())
               select fileItem).ToList();
      FileCompare myFileCompare = new FileCompare();
      // This query determines whether the two folders contain 
      // identical file lists, based on the custom file comparer 
      // that is defined in the FileCompare class. 
      return list1.SequenceEqual(list2, myFileCompare);
     }
    
    var filteredFiles = Directory
     .EnumerateFiles(path, "*.*") // .NET4 better than `GetFiles`
     .Where(
      // ignorecase faster than tolower...
      file => file.ToLower().EndsWith("aspx")
      || file.EndsWith("ascx", StringComparison.OrdinalIgnoreCase))
     .ToList();
    

      

    不要忘了新的.NET4Directory.EnumerateFiles对于性能提升(什么是目录之间的差异。与Directory.GetFiles?) “IGNORECASE”应该比“TOLOWER”快 或者,它可能会更快splitting的glob(至少它看起来更干净):

    "*.ext1;*.ext2".Split(';')
     .SelectMany(g => Directory.EnumerateFiles(path, g))
     .ToList();
    

    像这样的演示:

    void Main()
    {
     foreach(var f in GetFilesToProcess("c:\", new[] {".xml", ".txt"}))
      Debug.WriteLine(f);
    }
    private static IEnumerable<string> GetFilesToProcess(string path, IEnumerable<string> extensions)
    {
     return Directory.GetFiles(path, "*.*")
      .Where(f => extensions.Contains(Path.GetExtension(f).ToLower()));
    }
    

      

  • 相关阅读:
    读书笔记——高效能人士的七个习惯3
    读书笔记——高效能人士的七个习惯2(四类优先级)
    读书笔记——高效能人士的七个习惯1
    任正非最新谈话:吉田社长
    罗辑思维CEO脱不花:关于工作和成长,这是我的121条具体建议
    不需注释的生命
    读书笔记:《尽管去做——无压工作的艺术》摘抄
    C语言中变量名及函数名的命名规则与驼峰命名法
    回车”(carriage return)和”换行”(line feed)的区别和来历-(附:ASCII表)
    printf输出格式总结
  • 原文地址:https://www.cnblogs.com/libbybyron/p/5014979.html
Copyright © 2011-2022 走看看