Click to See Complete Forum and Search --> : Help for a C# newbie - Recursive File Search


altosax
February 23rd, 2006, 02:10 PM
I am new to .net - came from a PHP background and have little familiarity with the framework. But I am getting up to speed.

I would like to learn from example.

What if I would like to:

1) Get a list of all *.bat files in both the current directory AND all subdirectories
2) Populate listbox1 with the list of batch files - sorted alphabetically
3) Create an EventHandler so that when a user double clicks on the batch file it opens automagically

Care to show me your mastery?

darwen
February 23rd, 2006, 03:51 PM
Sounds like you already know what you need : you just need pointing in the right direction.

(1) The System.IO.Directory class will give you all the necessary functionality to do a recursive search. Look at the GetFiles() and GetDirectories() methods.

(2) You need to fill an ArrayList with all the filenames. Sorting should be left up to the ListBox.

(3) You can hook into the double click event on the ListBox and handle it.

Also, the ListBox.Items.Add takes an object - not a string. This means you can add any class you want into it. The displayed string as well as the sorting works on the ToString() virtual function from System.Object for whatever class you decide to use as your element class.

This should get you started.

When you reply please keep your questions to distinct problems rather than your last post - it has a "please write this for me" feel to it.

Darwen.