How to use Directory.GetFiles to get only gif types (or any other type)?

前端 未结 2 1386
太阳男子
太阳男子 2021-01-26 17:20

I\'m trying to filter files by extension (in particular case \".gif\") but GetFiles method for some reason does not find any files (even if there are definitely fil

相关标签:
2条回答
  • 2021-01-26 17:39

    You have 3 versions of "GetFiles" method. 2 of which includes a filter option: https://msdn.microsoft.com/en-us/library/system.io.directory.getfiles%28v=vs.110%29.aspx

    The version you were using accepts a "path". Not a filter.

    • Option 1 Directory.GetFiles with two arguments:

      var files = Directory.GetFiles(animatedgifsdirectory, "*.gif");
      
    • Option 2 DirectoryInfo.GetFiles instance method takes filter as single parameter:

      var directory = new DirectoryInfo(animatedgifsdirectory);
      var files = directory.GetFiles(animatedgifsdirectory, "*.gif");
      
    0 讨论(0)
  • 2021-01-26 17:45

    Use the search pattern overload on GetFiles:

    System.IO.Directory.GetFiles(@"C:\Windows", "*.gif");
    
    0 讨论(0)
提交回复
热议问题