如何:在 Visual Basic 中取得目錄的檔案集合

FileSystem.GetFiles 方法的多載會傳回唯讀的字串集合,代表了目錄內的檔案名稱:

如果找不到符合指定模式的檔案,會傳回空的集合。

列出目錄中的檔案

  • 使用其中一個 FileSystem.GetFiles 方法多載,並在 directory 參數中提供要搜尋的目錄名稱和路徑。 下列範例會傳回目錄中的所有檔案,並將它們新增到 ListBox1

    For Each foundFile As String In My.Computer.FileSystem.GetFiles(
      My.Computer.FileSystem.SpecialDirectories.MyDocuments)
    
        listBox1.Items.Add(foundFile)
    Next
    

穩固程式設計

以下條件可能會造成例外狀況:

另請參閱