在 VB.NET 中使用 WebClient 将子文件夹及其自己的文件复制到另一个文件夹
Copying subfolders with their own files to another folder with WebClient in VB.NET
我正在复制一个子文件夹数组和子文件夹的文件数组,过程成功,但复制的文件没有大小。 60 MB
文件在复制到另一个目录后变成了 0 bytes
,可能是哪里出了问题?
这是我的代码:
Dim WithEvents WebCopy As New WebClient
Dim foldersToCopy As New ArrayList()
Dim filesOfSub As New ArrayList()
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
foldersToCopy.Clear()
filesOfSub.Clear()
Dim src As String = Form2.TextBox1.Text
Dim dest As String = Form2.TextBox2.Text
Dim di As DirectoryInfo = New DirectoryInfo(src)
Dim dii As DirectoryInfo = New DirectoryInfo(dest)
' this is where I get the subfolders and its files.
For Each sd In di.GetDirectories
For Each fi In sd.GetFiles
If Not Directory.Exists(dest & "\" & sd.Name) And Not File.Exists(dest & "\" & fi.Name) Then
foldersToCopy.Add(sd.Name)
filesOfSub.Add(fi.Name)
End If
Next
Next
' this is where I create the subfolders
For i = 0 To foldersToCopy.Count - 1
If Not Directory.Exists(dest & "\" & dii.Name) Then
Directory.CreateDirectory(dest & "\" & foldersToCopy(i))
End If
Next
' this is where I copy the files of subfolders
For i = 0 To filesOfSub.Count - 1
Dim WebCopy As WebClient = New WebClient
WebCopy.DownloadFileAsync(New Uri(src & "\" & filesOfSub(i)), dest & "\" & foldersToCopy(i) & "\" & filesOfSub(i))
Next
End Sub
顺便说一句,我使用 WebClient
是因为其流畅的进度条和方便的处理程序使用。
感谢上帝,我找到了答案!
WebCopy.DownloadFileAsync(New Uri(src & "\" & foldersToCopy(i) & "\" & filesOfSub(i)), dest & "\" & foldersToCopy(i) & "\" & filesOfSub(i))
这应该是我的 DownloadFileAsync
复印机的正确形式。
我正在复制一个子文件夹数组和子文件夹的文件数组,过程成功,但复制的文件没有大小。 60 MB
文件在复制到另一个目录后变成了 0 bytes
,可能是哪里出了问题?
这是我的代码:
Dim WithEvents WebCopy As New WebClient
Dim foldersToCopy As New ArrayList()
Dim filesOfSub As New ArrayList()
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
foldersToCopy.Clear()
filesOfSub.Clear()
Dim src As String = Form2.TextBox1.Text
Dim dest As String = Form2.TextBox2.Text
Dim di As DirectoryInfo = New DirectoryInfo(src)
Dim dii As DirectoryInfo = New DirectoryInfo(dest)
' this is where I get the subfolders and its files.
For Each sd In di.GetDirectories
For Each fi In sd.GetFiles
If Not Directory.Exists(dest & "\" & sd.Name) And Not File.Exists(dest & "\" & fi.Name) Then
foldersToCopy.Add(sd.Name)
filesOfSub.Add(fi.Name)
End If
Next
Next
' this is where I create the subfolders
For i = 0 To foldersToCopy.Count - 1
If Not Directory.Exists(dest & "\" & dii.Name) Then
Directory.CreateDirectory(dest & "\" & foldersToCopy(i))
End If
Next
' this is where I copy the files of subfolders
For i = 0 To filesOfSub.Count - 1
Dim WebCopy As WebClient = New WebClient
WebCopy.DownloadFileAsync(New Uri(src & "\" & filesOfSub(i)), dest & "\" & foldersToCopy(i) & "\" & filesOfSub(i))
Next
End Sub
顺便说一句,我使用 WebClient
是因为其流畅的进度条和方便的处理程序使用。
感谢上帝,我找到了答案!
WebCopy.DownloadFileAsync(New Uri(src & "\" & foldersToCopy(i) & "\" & filesOfSub(i)), dest & "\" & foldersToCopy(i) & "\" & filesOfSub(i))
这应该是我的 DownloadFileAsync
复印机的正确形式。