Visual BasicNET How to Browse Files Folders and Directories
Let's create a simple application that browsesthe available drives on the computer, the folders, and files. I've created a Windows Forms application withthree ListBoxes. The first list, driveList, is going to contain a list of all the driveson 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 ofthose folders, the program will list all of the files contained within that folder.
First, we need to populate the driveList listwhen the form opens. So, I'm going to doubleclick 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 withinthe Form.Load event handler. This loop is going to iterate through the DriveInfo.GetDrivesstatic method. That returns a collection of DriveInfo objects. Each one is going to benamed di. As I iterate through each of these, I'm going to add them to the driveList listboxby calling the Items.Add method.
Back to the form, it's time for me to populatethe foldersList list box when the user clicks a drive. I'm going to doubleclick driveListto 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 anyitems that already exist within the foldersList list box. Next, I'm going to create a trycatch block.This is important here because, if the user selects a drive that is not available, forexample, a memory card reader that does not
have a memory card inserted into it, it willthrow 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 thework of listing all the subfolders. First, I'm going to create an instance of the DriveInfoclass that corresponds to the drive that the user selected. I'll call it drive. Now, toretrieve it, I'm going to use the DirectCast method. DirectCast casts, which is anotherword for converts, a generic object into the type that we actually need. So, to retrievethe selected item I need to call driveList.SelectedItem. Notice that I'm also specifying the classthat it needs to be converted into.
With this method here, DirectCast returnsan 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 relatesto the C:\ drive. Now I can create a For Each loop that iteratesthrough the Drive.RootDirectory.GetDirectories method, which corresponds to all of the rootdirectories for the drive. Within that For Each loop I need to add each of the DirectoryInfoinstances, which I called dirInfo, to the foldersList. So, the last thing I need to do is to populatethe filesList list box whenever the user clicks
the foldersList. So, I'll doubleclick thisto again create a SelectedIndexChanged event handler. As before, the first thing I needto do is to clear the items from the list. Now, I'm going to create a DirectoryInfo instancethat relates to the selected directory. As before I'm going to use DirectCast to castit to a DirectoryInfo instance. And, one last For Each loop. This time, I'm going to iteratethrough 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 beable to run it and browse the contents of the C:\ drive. ing C:\ loaded the foldersListlist box with all the folders in the root