leora
leora

Reputation: 196429

How to get all files in a directory with certain extension in C#?

I am using this to get all files in a directory:

string[] files = Directory.GetFiles(sourceDirectory_);

But is there a way to get all files that end with "jpg" in one line without doing an

if (file.endswidth("jpg") 

check?

Upvotes: 3

Views: 5293

Answers (2)

Petros
Petros

Reputation: 8992

You can provide the search pattern as a second parameter to GetFiles:

string[] files = Directory.GetFiles(sourceDirectory_, "*.jpg");

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1499770

Directory.GetFiles (sourceDirectory_, "*.jpg")

See the MSDN docs for this overload for more details.

Upvotes: 8

Related Questions