tags:

views:

205

answers:

2

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?

+5  A: 
Directory.GetFiles (sourceDirectory_, "*.jpg")

See the MSDN docs for this overload for more details.

Jon Skeet
this will fail, if the extension is JPG. Not a good practice.
Um? Since when does Windows do case-sensitive filename comparisons?
Pontus Gagge
As Pontus says, this method is case-insensitive. I've just checked it with exactly the situation you describe, prashant_sp - it picked up "a.jpg" and "b.JPG" with no problems.
Jon Skeet
+3  A: 

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

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