如果目标目录中不存在该文件夹,则通过创建文件夹将文件从一个目录复制到另一个目录时出现问题.
例:
C:\temp\test\1.txtC:\Data\如果C:\Data\不包含"temp"或"test"文件夹,则应在复制前创建文件夹1.txt.
复制到 C:\Data\temp\test\1.txt
以下是我的代码.但它不起作用..
Private Sub btnBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackup.Click
Dim sourcepath As String = "C:\temp\test\1.txt"
Dim DestPath As String = "C:\Data\"
CopyDirectory(sourcepath, DestPath)
End Sub
Private Shared Sub CopyDirectory(sourcePath As String, destPath As String)
If Not Directory.Exists(destPath) Then
Directory.CreateDirectory(destPath)
End If
For Each file__1 As String In Directory.GetFiles(sourcePath)
Dim dest As String = Path.Combine(destPath, Path.GetFileName(file__1))
File.Copy(file__1, dest)
Next
For Each folder As String …Run Code Online (Sandbox Code Playgroud)