How can Directory.Getfiles() multi searchpattern filters c#

bmurat picture bmurat · Jan 15, 2013 · Viewed 44.8k times · Source

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

I have a string array:

string[] pattern={"*.jpg","*.txt","*.asp","*.css","*.cs",.....};

this string pattern

string[] dizin = Directory.GetFiles("c:\veri",pattern);

How dizin variable C:\veri directories under the directory of files assign?

Answer

sa_ddam213 picture sa_ddam213 · Jan 15, 2013

You could use something like this

string[] extensions = { "jpg", "txt", "asp", "css", "cs", "xml" };

string[] dizin = Directory.GetFiles(@"c:\s\sent", "*.*")
    .Where(f => extensions.Contains(f.Split('.').Last().ToLower())).ToArray();

Or use FileInfo.Extension bit safer than String.Split but may be slower

string[] extensions = { ".jpg", ".txt", ".asp", ".css", ".cs", ".xml" };

string[] dizin = Directory.GetFiles(@"c:\s\sent", "*.*")
    .Where(f => extensions.Contains(new FileInfo(f).Extension.ToLower())).ToArray();

Or as juharr mentioned you can also use System.IO.Path.GetExtension

string[] extensions = { ".jpg", ".txt", ".asp", ".css", ".cs", ".xml" };

string[] dizin = Directory.GetFiles(@"c:\s\sent", "*.*")
    .Where(f => extensions.Contains(System.IO.Path.GetExtension(f).ToLower())).ToArray();