Search for files with specific extension

  • Thread starter Thread starter Merrit
  • Start date Start date
M

Merrit

Hi there,

I'm looking for a pretty easy way to search a folder for all contained
files with a specific extension and show the filenames in a listbox or
sth similar.

How can do that without using an OpenFileDialog?

Cheers,
Merrit
 
You can try to PInvoke FindFirstFile and FindNextFile to achieve this.

[DllImport("kernel32", CharSet=CharSet.Unicode)]
public static extern IntPtr FindFirstFile(string lpFileName, out
WIN32_FIND_DATA lpFindFileData);

[DllImport("kernel32", CharSet=CharSet.Unicode)]
public static extern bool FindNextFile(IntPtr hFindFile, out
WIN32_FIND_DATA lpFindFileData);

Hope this helps,
Cheers,
Arun.
www.innasite.com
 
System.IO.Directory.GetFiles("somefolder", "*.ext")

Returns an array of strings with the full paths of matching files in the
specified folder.

Peter
 
Peter said:
System.IO.Directory.GetFiles("somefolder", "*.ext")

Returns an array of strings with the full paths of matching files in the
specified folder.

Peter
Thanks a lot Peter. This solution was much easier and worked perfectly well.

Cheers,
Merrit
 
You'll have to sort them, using what you learned in your programming classes
in college...

Paul T.

Peter Foot [MVP] a écrit :
System.IO.Directory.GetFiles("somefolder", "*.ext")

And how to get them sorted by Date or by Size ?

thanx.
 
LOL! Nevertheless, it could be done without knowing about "Binary sort",
"Bubble sort", "Shell sort" or something :)

Here is example how to sort files by its size:

private class SizeSorter : IComparer
{
int IComparer.Compare(object a, object b)
{
FileInfo afi = new FileInfo(a as string);
FileInfo bfi = new FileInfo(b as string);
return afi.Length.CompareTo(bfi.Length);
}
}


....

string [] f = Directory.GetFiles("currentdir", "*.*");
ArrayList al = new ArrayList(f);
al.Sort(new SizeSorter());

"al" will contain the sorted files.
 
Back
Top