该方法 GetFiles 返回表示文件的路径名称的字符串的只读集合。 可以使用参数 wildCards
指定特定模式。
如果未找到匹配的文件,则返回空集合。
可以使用该方法 CopyFile 将文件复制到目录。
将具有特定模式的文件复制到目录
GetFiles
使用该方法返回文件列表。 此示例返回指定目录中的所有.rtf文件。For Each foundFile As String In My.Computer.FileSystem.GetFiles( My.Computer.FileSystem.SpecialDirectories.MyDocuments, Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly, "*.rtf")
使用
CopyFile
方法复制文件。 本示例将文件复制到名为testdirectory
的目录里。My.Computer.FileSystem.CopyFile(foundFile, "C:\testdirectory\" & My.Computer.FileSystem.GetName(foundFile))
使用
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 安全性
以下条件可能会导致异常:
路径对于以下原因之一无效:它是一个长度为零的字符串,它只包含空格,它包含无效字符,或者它是设备路径(以 \\.\) 开头(ArgumentException)。
路径无效,因为它是
Nothing
(ArgumentNullException)。目录不存在(DirectoryNotFoundException)。
目录指向现有文件(IOException)。
路径超过系统定义的最大长度(PathTooLongException)。
路径中的文件或目录名称包含冒号(:)或格式无效(NotSupportedException)。
用户缺少查看路径所需的权限(SecurityException)。 用户缺少必要的权限(UnauthorizedAccessException)。