FileSystem.GetFiles 方法

定義

回傳一個只讀的字串集合,代表目錄中檔案名稱。

多載

名稱 Description
GetFiles(String)

回傳一個只讀的字串集合,代表目錄中檔案名稱。

GetFiles(String, SearchOption, String[])

回傳一個只讀的字串集合,代表目錄中檔案名稱。

GetFiles(String)

回傳一個只讀的字串集合,代表目錄中檔案名稱。

public:
 static System::Collections::ObjectModel::ReadOnlyCollection<System::String ^> ^ GetFiles(System::String ^ directory);
public static System.Collections.ObjectModel.ReadOnlyCollection<string> GetFiles(string directory);
static member GetFiles : string -> System.Collections.ObjectModel.ReadOnlyCollection<string>
Public Shared Function GetFiles (directory As String) As ReadOnlyCollection(Of String)

參數

directory
String

目錄待查。

傳回

從指定目錄中只讀的檔案名稱集合。

例外狀況

該路徑不成立,原因之一是:它是零長度字串;它僅包含空白;它包含無效字元;或者是裝置路徑(以 \\.\ 開頭)。

directoryNothing

搜尋目錄不存在。

directory 指向一個現有檔案。

路徑超過系統定義的最大長度。

路徑中的檔案或目錄名稱包含冒號(:),或格式無效。

使用者缺乏必要的權限來查看路徑。

使用者缺乏必要的權限。

範例

以下範例回傳目錄中的所有檔案並將其加入 ListBox1

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

這個例子要求你的表格上必須有名字ListBoxListBox1

此範例會回傳目錄中所有副檔名 .txt 的檔案,並將其加入 ListBox1

For Each foundFile As String In My.Computer.FileSystem.GetFiles(
         My.Computer.FileSystem.SpecialDirectories.MyDocuments,
         FileIO.SearchOption.SearchTopLevelOnly, "*.txt")
    ListBox1.Items.Add(foundFile)
Next

這個例子要求你的表格上必須有名字ListBoxListBox1

備註

若未找到符合指定模式的檔案,則回傳為空集合。

下表列出與此 My.Computer.FileSystem.GetFiles 方法相關的任務範例。

看!
把檔案集合放到目錄裡 如何:在 Visual Basic
在目錄中尋找具有特定模式的檔案 如何:在 Visual Basic

另請參閱

適用於

GetFiles(String, SearchOption, String[])

回傳一個只讀的字串集合,代表目錄中檔案名稱。

public:
 static System::Collections::ObjectModel::ReadOnlyCollection<System::String ^> ^ GetFiles(System::String ^ directory, Microsoft::VisualBasic::FileIO::SearchOption searchType, ... cli::array <System::String ^> ^ wildcards);
public static System.Collections.ObjectModel.ReadOnlyCollection<string> GetFiles(string directory, Microsoft.VisualBasic.FileIO.SearchOption searchType, params string[] wildcards);
static member GetFiles : string * Microsoft.VisualBasic.FileIO.SearchOption * string[] -> System.Collections.ObjectModel.ReadOnlyCollection<string>
Public Shared Function GetFiles (directory As String, searchType As SearchOption, ParamArray wildcards As String()) As ReadOnlyCollection(Of String)

參數

directory
String

目錄待查。

searchType
SearchOption

是否要包含子資料夾。 預設值為 SearchOption.SearchTopLevelOnly

wildcards
String[]

圖案需要搭配。

傳回

從指定目錄中只讀的檔案名稱集合。

例外狀況

該路徑不成立,原因之一是:它是零長度字串;它僅包含空白;它包含無效字元;或者是裝置路徑(以 \\.\ 開頭)。

directoryNothing

搜尋目錄不存在。

directory 指向一個現有檔案。

路徑超過系統定義的最大長度。

路徑中的檔案或目錄名稱包含冒號(:),或格式無效。

使用者缺乏必要的權限來查看路徑。

使用者缺乏必要的權限。

範例

以下範例回傳目錄中的所有檔案並將其加入 ListBox1

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

這個例子要求你的表格上必須有名字ListBoxListBox1

此範例會回傳目錄中所有副檔名 .txt 的檔案,並將其加入 ListBox1

For Each foundFile As String In My.Computer.FileSystem.GetFiles(
         My.Computer.FileSystem.SpecialDirectories.MyDocuments,
         FileIO.SearchOption.SearchTopLevelOnly, "*.txt")
    ListBox1.Items.Add(foundFile)
Next

這個例子要求你的表格上必須有名字ListBoxListBox1

備註

若未找到符合指定模式的檔案,則回傳為空集合。

下表列出與此 My.Computer.FileSystem.GetFiles 方法相關的任務範例。

看!
把檔案集合放到目錄裡 如何:在 Visual Basic
在目錄中尋找具有特定模式的檔案 如何:在 Visual Basic

另請參閱

適用於