DirectoryInfo.GetFiles, How to get different types of files in C #

How to find file types * .gif and * .jpg using a function DirectoryInfo.GetFilesin C #?

When I try this code:

string pattern = "*.gif|*.jpg";
FileInfo[] files = dir.GetFiles(pattern);

Exception "Invalid characters on the way." rushes.

+5
source share
4 answers

You cannot do this. You need to use a method GetFiles()for each of them. Or you can use an array for your extensions and then check each one of them, but you also need this method more than once.

Check out these questions;

+2
source

, :

var extensions = new[] { "*.gif", "*.jpg" };
var files = extensions.SelectMany(ext => dir.GetFiles(ext));
+6

, GetFiles . ...

var exts = new string[] { "*.gif", "*.jpg" };
foreach (var ext in exts) {
  var files = dir.GetFiles(ext);
}

*.* , .

+1

, where .

DirectoryInfo directoryInfo = new DirectoryInfo(filePath);
FileInfo[] files = directoryInfo.GetFiles().Where(f => f.Extension == ".gif" || f.Extension == ".jpg").ToArray();
0

All Articles