DirectoryInfo.GetFiles()

  • Thread starter Thread starter Pluto
  • Start date Start date
P

Pluto

Hi,

I want to get a list of files matching certain extensions, such as
"*.jpg;*.gif" (i.e. all files ending with .jpg and .gif).

I was using DirectoryInfo.GetFiles(string) method. Unfortunately it appears
that I cannot have something like:
dirInfo.GetFiles("*.jpg;*.gif"); // where dirInfo is of type
DirectoryInfo

Help.

Thanks.
 
Pluto said:
I want to get a list of files matching certain extensions, such as
"*.jpg;*.gif" (i.e. all files ending with .jpg and .gif).

I was using DirectoryInfo.GetFiles(string) method. Unfortunately it appears
that I cannot have something like:
dirInfo.GetFiles("*.jpg;*.gif"); // where dirInfo is of type
DirectoryInfo

Why not just ask for each of them separately and then combine the
results? It would be pretty easy to write a utility method to do
precisely that.
 
I couldn't find any method in the DirectoryInfo that
suffices your requirement. I've pasted a some stuff that
you may want to have a look at.

Its kinda brute....

/////////////////////////////////////////////////////////
DirectoryInfo directoryInfo = new DirectoryInfo ("c:\\");
FileInfo [] fileInfo = directoryInfo.GetFiles ();
ArrayList arrayList = new ArrayList ();
string [] filter = {".exe", ".bat", ".txt", ".log"};

foreach (FileInfo fi in fileInfo)
foreach (string s in filter)
if (s == fi.Extension) arrayList.Add (fi);

return (FileInfo[])arrayList.ToArray(typeof (FileInfo));
////////////////////////////////////////////////////////
 
Thanks.


Jon Skeet said:
Why not just ask for each of them separately and then combine the
results? It would be pretty easy to write a utility method to do
precisely that.
 
Thanks.


Amit Khot said:
I couldn't find any method in the DirectoryInfo that
suffices your requirement. I've pasted a some stuff that
you may want to have a look at.

Its kinda brute....

/////////////////////////////////////////////////////////
DirectoryInfo directoryInfo = new DirectoryInfo ("c:\\");
FileInfo [] fileInfo = directoryInfo.GetFiles ();
ArrayList arrayList = new ArrayList ();
string [] filter = {".exe", ".bat", ".txt", ".log"};

foreach (FileInfo fi in fileInfo)
foreach (string s in filter)
if (s == fi.Extension) arrayList.Add (fi);

return (FileInfo[])arrayList.ToArray(typeof (FileInfo));
////////////////////////////////////////////////////////

-----Original Message-----
Hi,

I want to get a list of files matching certain extensions, such as
"*.jpg;*.gif" (i.e. all files ending with .jpg and .gif).

I was using DirectoryInfo.GetFiles(string) method. Unfortunately it appears
that I cannot have something like:
dirInfo.GetFiles("*.jpg;*.gif"); // where dirInfo is of type
DirectoryInfo

Help.

Thanks.


.
 
Back
Top