Search for files with specific extension

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
 
A

Arun

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
 
P

Peter Foot [MVP]

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

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

Peter
 
M

Merrit

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
 
P

Paul G. Tobey [eMVP]

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.
 
S

Sergey Bogdanov

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top