VBNET 拷贝文件夹
时间: 2023-06-09 14:02:34 浏览: 116
VB 复制文件夹
4星 · 用户满意度95%
在 VB.NET 中拷贝文件夹可以使用以下代码:
```
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sourcePath As String = "C:\SourceFolder"
Dim targetPath As String = "D:\TargetFolder"
CopyDirectory(sourcePath, targetPath)
MessageBox.Show("文件夹拷贝完成!")
End Sub
Private Sub CopyDirectory(ByVal sourcePath As String, ByVal targetPath As String)
Directory.CreateDirectory(targetPath)
For Each file As String In Directory.GetFiles(sourcePath)
Dim fileName As String = Path.GetFileName(file)
Dim destFile As String = Path.Combine(targetPath, fileName)
File.Copy(file, destFile, True)
Next
For Each directory As String In Directory.GetDirectories(sourcePath)
Dim dirName As String = Path.GetFileName(directory)
Dim destDir As String = Path.Combine(targetPath, dirName)
CopyDirectory(directory, destDir)
Next
End Sub
End Class
```
其中 `sourcePath` 表示源文件夹路径,`targetPath` 表示目标文件夹路径。代码中首先创建目标文件夹,然后获取源文件夹中所有文件,逐一拷贝到目标文件夹中。最后获取源文件夹中所有子文件夹,递归调用 `CopyDirectory` 方法拷贝子文件夹。
阅读全文