Bagikan melalui


Closer Look: Parsing File Paths

When you work with files in your application, you often have to refer to only parts of the file path. For example, you might want to display the name of a file without its path. You can use the My.Computer.FileSystem Object to perform file parsing tasks.

You can retrieve the path of a file by using the My.Computer.FileSystem.GetParentPath Method. You can retrieve the name of the file without its path by using the My.Computer.FileSystem.GetName Method.

For example, in the Picture Viewer application described in Retrieving the Names of Files in a Folder, instead of displaying the full path of each picture, you could remove the path and display only the name of the file. In this lesson, you will populate a list box with the names of all files in the Documents folder.

Note

In Windows XP, there are special folders that 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.

Try it!

To parse the file names

  1. On the File menu, click NewProject.

  2. On the Templates pane of the New Project dialog box, click Windows Application.

  3. In the Name box, type Parse File Path, and then click OK.

    A new Windows Forms project opens.

  4. Add a ListBox control to the form, leaving the default name ListBox1.

  5. Add a Button control to the form, leaving the default name Button1, and change the Text property to Load.

  6. Right-click the form and click View Code.

  7. Add the following code under the Public Class Form1 statement at the top of the code file. This code creates a variable for the Documents folder.

    Dim FilePath As String = _
        My.Computer.FileSystem.SpecialDirectories.MyDocuments
    
  8. In the left drop-down list (Class Name), click Button1.

  9. In the right drop-down list (Method Name), click Click to create the Button1_Click event handler.

  10. In the Button1_Click event handler, add the following code. This code clears all items in the list box and then searches through the files in the Documents folder and adds their names to the list box. It uses the My.Computer.FileSystem.GetName Method to retrieve the name of the file without the folder path.

    Dim FoundFile As String 
    Dim FileName As String 
    
    ' Clear the list box. 
    Me.ListBox1.Items.Clear()
    
    ' Add each file in the Documents folder to list box. 
    For Each FoundFile In My.Computer.FileSystem.GetFiles( _
        FilePath, FileIO.SearchOption.SearchTopLevelOnly)
    
        ' Add only the name of each found file to the list box.
        FileName = My.Computer.FileSystem.GetName(FoundFile)
        Me.ListBox1.Items.Add(FileName)
    
    Next
    
  11. Press F5 to run the program.

  12. When the form appears, click the Load button.

    The file name (without the path) of each file in the Documents folder appears in the list box.

  13. Close the Parse File Path application.

Combining the File Name and Folder Path

If you want to perform an action on the listed files, such as opening a file, you must supply both the name and path of the file. You can use the My.Computer.FileSystem.CombinePath Method to combine the name with its path. The following example shows how to display the path and name of a file in a message box when you click the name in a list box.

To combine the file name and folder path

  1. In Designer view, double-click the ListBox to enter the default SelectedIndexChanged event handler.

  2. In the ListBox1_SelectedIndexChanged event handler, add the following code. This code combines the folder path and file name and displays the combined name in a message box.

    MsgBox(My.Computer.FileSystem.CombinePath( _
        FilePath, Me.ListBox1.SelectedItem))
    
  3. Press F5 to run the program.

  4. When the form appears, click the Load button.

  5. Click a file name in the list box.

  6. A message box displays the name and path of the file.

  7. Close the application.

Next Steps

In this lesson, you learned how to parse a file path to separate the name of the file from its path. You also learned how to combine a path and file name. In the next lesson, you will learn how to write to a text file.

Next Lesson: Writing to a Text File

See Also

Tasks

Retrieving the Names of Files in a Folder

How to: Parse File Paths in Visual Basic

Concepts

Development with My

Other Resources

Using the File System: Writing to and Reading from Files

Visual Basic Guided Tour