Let's create a simple application that browses the available drives on the computer, the
folders, and files.
I've created a Windows Forms application with three ListBoxes. The first list, driveList,
is going to contain a list of all the drives on the computer (like the C: drive, the D:
drive, or the E: drive).
When the user clicks one of those drives, it will show all of the folders at the root
of that drive. When the user clicks one of those folders, the program will list all of
the files contained within that folder.
First, we need to populate the driveList list when the form opens. So, I'm going to double-click
the form to open the Form.Load event handler.
First, I need to import the System.IO namespace. This namespace contains all of the classes
used for accessing the file system.
Next, I'm going to use a For Each loop within the Form.Load event handler. This loop is
going to iterate through the DriveInfo.GetDrives static method. That returns a collection of
DriveInfo objects. Each one is going to be named di. As I iterate through each of these,
I'm going to add them to the driveList listbox by calling the Items.Add method.
Back to the form, it's time for me to populate the foldersList list box when the user clicks
a drive. I'm going to double-click driveList to open up the default event handler, which
handles the SelectedIndexChanged event handler. This event is called any time the user selects
a different item.
The first thing I need to do is to clear any items that already exist within the foldersList
list box. Next, I'm going to create a try-catch block.
This is important here because, if the user selects a drive that is not available, for
example, a memory card reader that does not have a memory card inserted into it, it will
throw an error. So, in the event handler I'm going to show the error in a MessageBox.
Within the Try block I'll actually do the work of listing all the sub-folders. First,
I'm going to create an instance of the DriveInfo class that corresponds to the drive that the
user selected. I'll call it drive. Now, to retrieve it, I'm going to use the DirectCast
method. DirectCast casts, which is another word for converts, a generic object into the
type that we actually need. So, to retrieve the selected item I need to call driveList.SelectedItem.
Notice that I'm also specifying the class that it needs to be converted into.
With this method here, DirectCast returns an instance of DriveInfo, which is why I can
create the new object in this way. For example, if the user clicks the C:\ drive, this will
create an instance of DriveInfo that relates to the C:\ drive.
Now I can create a For Each loop that iterates through the Drive.RootDirectory.GetDirectories
method, which corresponds to all of the root directories for the drive. Within that For
Each loop I need to add each of the DirectoryInfo instances, which I called dirInfo, to the
foldersList.
So, the last thing I need to do is to populate the filesList list box whenever the user clicks
the foldersList. So, I'll double-click this to again create a SelectedIndexChanged event
handler. As before, the first thing I need to do is to clear the items from the list.
Now, I'm going to create a DirectoryInfo instance that relates to the selected directory. As
before I'm going to use DirectCast to cast it to a DirectoryInfo instance. And, one last
For Each loop. This time, I'm going to iterate through the DirectoryInfo.GetFiles collection,
which is a collection of FileInfo objects, and add each one to the filesList listBox.
Now, if I did all this right, I should be able to run it and browse the contents of
the C:\ drive. Clicking C:\ loaded the foldersList list box with all the folders in the root
of the C:\ drive. I'll click the Windows folders, and the filesList list box now shows each
of the files in my Windows folder.