Retrieving the Names of Files in a Folder
In this lesson, you will learn how to retrieve the names of files in a folder by using the My.Computer.FileSystem Object.
A folder is an area in Microsoft Windows where you can store your files. Microsoft Windows has some special folders that help you organize files into categories, such as documents, pictures, and music files.
In Windows XP, these folders are named MyDocuments, MyPictures, and MyMusic. In Windows Vista, these folders do not have "My" in the names; instead, they are named Documents, Pictures, and Music. However, the code in this lesson will run on both Windows XP and Windows Vista.
In this lesson, you will create a Picture Viewer application in which you can display picture files found in the Pictures folder. When you select an item in a ListBox, the corresponding graphic is displayed in a PictureBox control.
You can check for the existence of a particular file by using the My.Computer.FileSystem.FileExists Method and specifying the folder path and file name.
If you want to retrieve the names of all files in a folder, you can use the My object to get a collection of all the files that reside in a particular folder. You can also look for a file that has a unique name by specifying a pattern.
Use the My.Computer.FileSystem.GetFiles Method to return the files in the specified folder. You can pass an optional list of wildcards to indicate a pattern of files that you want to return. For example, you can retrieve the names of all files that are Graphics Interchange Format (GIF) images by indicating that the files should have a .gif extension (*.gif*). You can add more graphic format types, such as Joint Photographic Experts Group (JPEG), by indicating the (*.jpg*) extension and separating the extensions with a comma.
You can specify a special folder, such as Pictures, by using the My.Computer.FileSystem.SpecialDirectories Object. This returns each file's name, including the full path. You can then loop through the returned file names and add each file name to the ListBox.
To create the application's user interface (UI)
On the File menu, click New Project.
On the Templates pane of the New Project dialog box, click Windows Application.
In the Name box, type Picture Viewer, and then click OK.
A new Windows Forms project opens.
Click the form and change the following properties in the Properties Window:
Add a ListBox control to the form, leaving the default name ListBox1.
Click the ListBox and change the following properties:
Add a PictureBox control to the form, leaving the default name PictureBox1.
Click the PictureBox and change the following properties:
Add a Button control to the form.
Change the following properties of the Button:
After the UI for Picture Viewer is created, you can write code to retrieve the file names.
To retrieve file names
Double-click the Load Pictures button to enter the default event handler in the Code Editor.
In the LoadPictures_Click event handler, add the following code to retrieve all GIF and JPEG files in the Pictures folder and display their file names in the list box. The code specifies that subfolders within the Pictures folder should not be searched by indicating the SearchTopLevelOnly search option.
' Clear the list box and the picture box. Me.ListBox1.Items.Clear() Me.PictureBox1.ImageLocation = "" ' Add each image in the Pictures directory to list box. For Each foundImage As String In _ My.Computer.FileSystem.GetFiles( _ My.Computer.FileSystem.SpecialDirectories.MyPictures, _ FileIO.SearchOption.SearchTopLevelOnly, "*.gif*", "*.jpg*") Me.ListBox1.Items.Add(foundImage) Next
Add the following code to display a message box to the user if there are no images in the Pictures folder.
Press F5 to run the program.
When the form appears, click the Load Pictures button.
The file name and path of each GIF or JPEG file in the Pictures directory is displayed in the list box.
Close the Picture Viewer application.
In this example, the path to the files in the Pictures directory is long, and you will have to scroll to the right to see the actual file names. You could alternatively parse each string by separating the folder path from the file name. For more information, see Closer Look: Parsing File Paths.
Adding the file names to the list box would be more interesting if you could also preview each picture. In this procedure, you will write code in the SelectedIndexChanged event handler of ListBox1 to display the selected picture in the PictureBox.
To view the pictures
In the Code Editor, in the Class Name drop-down list, click ListBox1.
In the Method Name drop-down list, click SelectedIndexChanged to create the event handler.
In the ListBox1_SelectedIndexChanged event handler, add the following code to display the selected picture.
Press F5 to run the program.
Click the button to load the files from the Pictures directory, and then click a file name in the list box. Each time you click an item in the list, a new picture appears.
Close the Picture Viewer application and save your project. You will use this project in the next lesson.