반응형

How to get multiple files with different extension?



You can't set more search filters, because GetFiles only accepts a single search pattern. Instead, you can call GetFiles with no pattern, and filter the results in code, using Lambda expressions:



using System.Linq;



   public YourMethodToStart()
    {
      FileInfo[] fiels = GetFilesFromFolder(@"C:\1");
    }

    private FileInfo[] GetFilesFromFolder(string path)
    {     
      string[] extensions = new[] { ".txt", ".zip", ".exe" };
      DirectoryInfo dInfo = new DirectoryInfo(path);

      FileInfo[] files = dInfo.GetFiles()
        .Where(f => extensions.Contains(f.Extension.ToLower()))
        .ToArray();
      return files;
    }



ref : https://social.msdn.microsoft.com/Forums/en-US/3e29882f-238e-473b-91c1-8639a51d39cd/get-multiple-files-with-different-extension?forum=csharplanguage

반응형

+ Recent posts