Will_G
Will_G

Reputation: 269

How can I GetFiles only of 2 types?

Code:

string[] arrFileEntries = Directory.GetFiles(MapPath("..\\Pictures\\"), "*.jpg", "*.png");

Error:

The closest match to the overloaded method System.IO.Directory.GetFiles (string, string, System.IO.SearchOption) has some invalid arguments

Upvotes: 1

Views: 84

Answers (2)

Ankush Jain
Ankush Jain

Reputation: 1527

try the following.......get data in two array and merge them

string[] array1 = Directory.GetFiles(@"C:\", "*.jpg");
 string[] array2 = Directory.GetFiles(@"C:\", "*.png");

 string[] newArray = new string[array1.Length + array2.Length];
 Array.Copy(array1, newArray, array1.Length);
 Array.Copy(array2, 0, newArray, array1.Length, array2.Length);

Upvotes: 1

Mahesh Alle
Mahesh Alle

Reputation: 84

You try below code using linqu:-

var files = Directory.GetFiles(MapPath("..\\Pictures\\"), "*.*", SearchOption.AllDirectories)
            .Where(s => s.EndsWith(".jpg") || s.EndsWith(".png"));

Upvotes: 2

Related Questions