如何:在 Visual Basic 中分析文件路径

分析文件路径时,该 FileSystem 对象提供了许多有用的方法。

  • CombinePath 方法接受两个路径并返回格式正确的组合路径。

  • GetParentPath 方法将返回所提供的路径的父级的绝对路径。

  • 该方法 GetFileInfo 返回可以 FileInfo 查询的对象,以确定文件的属性,例如其名称和路径。

不要根据文件扩展名决定文件的内容。 例如,文件Form1.vb可能不是 Visual Basic 源文件。

确定文件的名称和路径

  • 使用DirectoryName对象的NameFileInfo属性来确定文件的名称和路径。 此示例确定名称和路径并显示它们。

    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)
    

合并文件名和目录以创建完整路径

  • CombinePath使用该方法,提供目录和名称。 此示例采用在上一个示例中创建的字符串 folderPathfileName,将它们合并并显示结果。

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

另请参阅