Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Search for files with specific extension

3 views
Skip to first unread message

Merrit

unread,
Jun 14, 2005, 5:58:26 AM6/14/05
to
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

Arun

unread,
Jun 14, 2005, 6:10:03 AM6/14/05
to
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

Peter Foot [MVP]

unread,
Jun 14, 2005, 6:42:19 AM6/14/05
to
System.IO.Directory.GetFiles("somefolder", "*.ext")

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

Peter

--
Peter Foot
Windows Embedded MVP
http://www.inthehand.com | http://www.peterfoot.net |
http://www.opennetcf.org

"Merrit" <m.kr...@fhtw-berlin.de> wrote in message
news:3h7o1sF...@news.dfncis.de...

Merrit

unread,
Jun 14, 2005, 7:52:14 AM6/14/05
to
Peter Foot [MVP] wrote:
> 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

Cyrille37

unread,
Jun 21, 2005, 9:42:36 AM6/21/05
to
Peter Foot [MVP] a écrit :

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

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

thanx.

Paul G. Tobey [eMVP]

unread,
Jun 21, 2005, 11:01:11 AM6/21/05
to
You'll have to sort them, using what you learned in your programming classes
in college...

Paul T.

"Cyrille37" <cyri...@nospam.net> wrote in message
news:%23bcoWcm...@TK2MSFTNGP10.phx.gbl...

Sergey Bogdanov

unread,
Jun 21, 2005, 11:39:49 AM6/21/05
to
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.

--
Sergey Bogdanov
http://www.sergeybogdanov.com


Paul G. Tobey [eMVP] wrote:
> You'll have to sort them, using what you learned in your programming classes
> in college...
>
> Paul T.
>
> "Cyrille37" <cyri...@nospam.net> wrote in message
> news:%23bcoWcm...@TK2MSFTNGP10.phx.gbl...

> Peter Foot [MVP] a йcrit :

0 new messages