共用方式為


如何:將具有特定模式的檔案複製到 Visual Basic 中的目錄

方法 GetFiles 會傳回代表檔案路徑名稱的唯讀字串集合。 您可以使用 wildCards 參數來指定特定模式。

如果找不到相符的檔案,則會傳回空集合。

您可以使用 CopyFile 方法將檔案複製到目錄。

將具有特定模式的檔案複製到目錄

  1. GetFiles使用方法可傳回檔案清單。 這個範例會傳回指定目錄中的所有.rtf檔案。

    For Each foundFile As String In My.Computer.FileSystem.GetFiles(
        My.Computer.FileSystem.SpecialDirectories.MyDocuments,
        Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly, "*.rtf")
    
  2. 使用 CopyFile 方法來複製檔案。 此範例會將檔案複製到名為 的 testdirectory目錄。

    My.Computer.FileSystem.CopyFile(foundFile, "C:\testdirectory\" & My.Computer.FileSystem.GetName(foundFile))
    
  3. 使用 For 語句關閉 Next 語句。

    Next
    

範例

下列範例會以完整格式呈現上述代碼段,會將指定目錄中的所有.rtf檔案複製到名為 的 testdirectory目錄。

For Each foundFile As String In My.Computer.FileSystem.GetFiles(
    My.Computer.FileSystem.SpecialDirectories.MyDocuments,
    Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly, "*.rtf")

    My.Computer.FileSystem.CopyFile(foundFile, "C:\testdirectory\" & foundFile)
Next

.NET Framework 安全性

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

另請參閱