How to: Parse File Paths in Visual Basic

The My.Computer.FileSystem Object offers a number of useful methods when parsing file paths.

Do not make decisions about the contents of the file based on the file name extension. For example, the file Form1.vb may not be a Visual Basic source file.

To determine a file's name and path

  • Use the DirectoryName and Name properties of the FileInfo object to determine a file's name and path. This example determines the name and path and displays them.

    Dim testFile As System.IO.FileInfo
    testFile = My.Computer.FileSystem.GetFileInfo("C:\TestFolder1\test1.txt")
    Dim folderPath As String = testFile.DirectoryName
    MsgBox(folderPath)
    Dim fileName As String = testFile.Name
    MsgBox(fileName)
    

To combine a file's name and directory to create the full path

  • Use the CombinePath method, supplying the directory and name. This example takes the strings folderPath and fileName created in the previous example, combines them, and displays the result.

    Dim fullPath As String
    fullPath = My.Computer.FileSystem.CombinePath(folderPath, fileName)
    MsgBox(fullPath)
    

See Also

Tasks

How to: Get the Collection of Files in a Directory in Visual Basic

How to: Determine the Absolute Path of a File in Visual Basic

How to: Get Information About a File in Visual Basic

Reference

My.Computer.FileSystem Object

My.Computer.FileSystem.CombinePath Method

FileInfo

My.Computer.FileSystem.GetFileInfo Method