共用方式為


HOW TO:在 Visual Basic 中剖析檔案路徑

更新:2007 年 11 月

剖析檔案路徑時,My.Computer.FileSystem 物件會提供一些有用的方法。

請勿根據副檔名判斷檔案內容。例如,檔案 Form1.vb 可能不是 Visual Basic 原始程式檔。

若要判斷檔案的名稱和路徑

  • 請使用 FileInfo 物件的 DirectoryNameName 屬性,判斷檔案的名稱和路徑。此範例會判斷名稱和路徑,並加以顯示。

    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 方法,以提供目錄和名稱。此範例會採用在先前範例中建立的字串 folderPath 和 fileName,加以結合並顯示結果。

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

請參閱

工作

HOW TO:在 Visual Basic 中取得目錄的檔案集合

HOW TO:在 Visual Basic 中判斷檔案的絕對路徑

HOW TO:在 Visual Basic 中取得檔案的相關資訊

參考

My.Computer.FileSystem 物件

My.Computer.FileSystem.CombinePath 方法

FileInfo

My.Computer.FileSystem.GetFileInfo 方法