Can you call Directory.GetFiles() with multiple filters?

前端 未结 26 2785
逝去的感伤
逝去的感伤 2020-11-22 05:25

I am trying to use the Directory.GetFiles() method to retrieve a list of files of multiple types, such as mp3\'s and jpg\'s. I have t

26条回答
  •  余生分开走
    2020-11-22 06:08

    Make the extensions you want one string i.e ".mp3.jpg.wma.wmf" and then check if each file contains the extension you want. This works with .net 2.0 as it does not use LINQ.

    string myExtensions=".jpg.mp3";
    
    string[] files=System.IO.Directory.GetFiles("C:\myfolder");
    
    foreach(string file in files)
    {
       if(myExtensions.ToLower().contains(System.IO.Path.GetExtension(s).ToLower()))
       {
          //this file has passed, do something with this file
    
       }
    }
    

    The advantage with this approach is you can add or remove extensions without editing the code i.e to add png images, just write myExtensions=".jpg.mp3.png".

提交回复
热议问题