How to: Parse File Paths in Visual Basic

The FileSystem object offers a number of useful methods when parsing file paths.

  • The CombinePath method takes two paths and returns a properly formatted combined path.

  • The GetParentPath method returns the absolute path of the parent of the provided path.

  • The GetFileInfo method returns a FileInfo object that can be queried to determine the file's properties, such as its name and path.

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

FileSystem

CombinePath

FileInfo

GetFileInfo