我是VB.net的新手,我正在尝试使用进度条创建一个备份实用程序,将一组本地文件夹复制到单个备份目标(例如在usb上)。谷歌搜索过去几个小时后,我只能找到单个文件夹到单个目的地的示例。
有人能指出我的方向吗?
更新:
基于@Werdna的例子,我创建了一个简单的FOR EACH NEXT循环。但是,下一个问题是只将源目录中的文件复制到目标目录,而不是文件夹及其所有内容。任何人都可以看到我错在哪里?
Public Class Form1
Private Sub Start_Click(sender As Object, e As EventArgs) Handles Start.Click
Dim destination = "E:\Backup Folder"
Dim sources As New List(Of String)
sources.Add("D:\Profiles\Users\Desktop")
sources.Add("D:\Profiles\Users\Mail")
sources.Add("D:\Profiles\Users\Downloads")
For Each source As String In sources
My.Computer.FileSystem.CopyDirectory(source, destination)
Next
MessageBox.Show("Copy Completed")
End Sub
End Class
此外,使用FOR EACH NEXT循环计算要复制的文件数的最佳方法是什么?我希望将金额输出到标签,并在实用程序发展时将其用于进度条。
答案 0 :(得分:2)
这里有一些我已经快速写给你的东西,让你知道我如何完成你的任务。您需要在应用程序中添加2个列表框,文件夹打开对话框和几个按钮,这可能不是您要查找的内容,遗憾的是您无法使用其他对话框选择多个文件夹,但是,请看下面的内容,代码并不完全,因为总会有事情要做,但是这应该会带领你朝着正确的方向前进!
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each item In IO.Directory.GetDirectories("C:\")
ListBox1.Items.Add(item)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each item In ListBox1.SelectedItems
ListBox2.Items.Add(item & "\")
Next
ListBox1.SelectedItem = Nothing
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
ListBox2.Items.Remove(ListBox2.SelectedItem)
ListBox2.SelectedItem = Nothing
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each item In IO.Directory.GetDirectories(ListBox1.SelectedItem)
ListBox1.Items.Clear()
ListBox1.Items.Add(item)
Next
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
My.Computer.FileSystem.CreateDirectory(FolderBrowserDialog1.SelectedPath)
For Each item In ListBox2.Items
My.Computer.FileSystem.CopyDirectory(item, FolderBrowserDialog1.SelectedPath)
Next
MessageBox.Show("Copy Completed.")
End If
End Sub
End Class
我在大约10分钟内写了这个,所以我没有做任何改变来做一个progessbar,但如果这是你正在寻找的,那么我很乐意帮助你添加一个程序。快乐的编码!
更新 - 基于你的新问题
Public Class Form1
Dim NumberofFILEs As Integer = 0
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim Location1 = "C:\Backup Folder\TESTING\FOLDER"
Dim source As String = "C:\TESTING\FOLDER"
Dim source1 As String = "C:\TESTING1\FOLDER"
Dim Location2 = "C:\Backup Folder\TESTING1\FOLDER"
IO.Directory.CreateDirectory("C:\Backup Folder\TESTING\FOLDER")
IO.Directory.CreateDirectory("C:\Backup Folder\TESTING1\FOLDER")
For Each item In source
My.Computer.FileSystem.CopyDirectory(source, Location1, True)
Next
For Each item In source1
My.Computer.FileSystem.CopyDirectory(source1, Location2, True)
Next
MessageBox.Show("Completed")
For Each file In source1.Count & source.Count
NumberofFILEs += 1
Next
Label1.Text = NumberofFILEs
End Sub
结束班
根据需要编辑位置和目的地。同样,copydirectory末尾的True表示它会覆盖任何具有相同名称的文件,例如它会更新它们
答案 1 :(得分:0)
您可以将每个文件夹一次复制到目的地。遍历源文件夹并将每个文件夹复制到目标文件夹。