To demonstrate working with the file system, I've created a simple Windows Forms application
with three listboxes.
The first listbox, driveList, is going to contain a list of drives available on the
system. When the user selects one of these drives, the application will populate the
foldersList listbox, containing all the subfolders of that particular drive. The third listbox
will contain all of the files within that folder.
Note that this isn't extremely complex; you can't select subfolders for example. Ideally,
it would be implemented with a tree, but I selected this setup for simplicity.
So, the first thing we need to do when the application loads is to populate the driveList
list. I will do that in the Form1.Load event handler. Before we do anything, I need to
add the System.IO namespace that contains all of the different classes used for accessing
the file system.
Within the Form1.Load handler we need to create a foreach loop that iterates through all of
the drives and adds them to the drives list. We call DriveInfo.GetDrives. That method returns
a collection of DriveInfo objects. Now, we just need to add each object to the driveList
listbox.
With that done, we need to populate the foldersList listbox whenever the user selects a drive.
So, I will double-click on this to select the SelectedIndexChanged event handler for
that listbox. This event handler is called any time the user selects a different item
in the list box.
First, we want to clear any items that might already be in the folderList list. So, we
will call the Items.Clear method.
I'm going to put all of this into a try-catch block. This will enable me to catch any errors
that might occur if the user selects a drive that is not available. For example, a memory
card reader that does not have a memory card currently inserted. A simple try-catch block
that just displays the error message will suffice for our purposes.
Within the try, I will create a DriveInfo object by casting the selected object from
the list. If you look up to the foreach loop, you'll notice that I added DriveInfo items
to the driveList. When the user selects one, I can access that DriveInfo item using drivelist.SelectedItem.
However, SelectedItem, as you can see here, returns a generic object item. To be able
to access that as the original DriveInfo class, I need to cast it, which I do by putting DriveInfo
in parenthesis directly before the SelectedItem. This creates a DriveInfo object named Drive
that is the item that was selected up here. So, if the user clicks the C drive, the DriveInstance
represents the C:\ drive.
With that extracted, I can create a foreach loop that iterates through the directories
within the selected drive. I'm going to iterate through a collection of DirectoryInfo instances
and I'm going to access the RootDirectory member of the drive instance I just created,
and call GetDirectories. GetDirectories contains a collection of DirectoryInfo objects. Now,
I'm going to add those DirectoryInfo objects to the foldersList
items.
Now, when the application runs, it will populate the driveList with a list of available drives.
When the user selects a drive, it will populate the foldersList with a list of folders within
that drive. Now, we need to populate filesList with a list of files within that folder.
As I did before, I'll double-click to create the foldersList.SelectedIndexChanged event
handler. First, I'll clear any existing items by calling the filesList.Items.Clear() method.
Then, I'm going to create a DirectoryInfo instance by casting the selected item to an
instance of DirectoryInfo.
Another foreach loop will iterate through the DirectoryInfo.GetFiles collection, which
contains all the files in the selected directory. THat provides me a collection of FileInfo
objects. Now, I will add each of those to the Items
collection in the filesList.
Now, let's run our application and see if it works. I will click the C:\ drive. It populates
the foldersList listbox with a list of folders. Let's select the Windows folders. You can
see that it lists all of the items contained within that folder.