列出子文件夹的VB.net备份程序
第一次贴在这里,所以请温柔一点 我正在使用Visual Studio Express 2010 我正在尝试创建一个备份程序,每晚将特定文件夹和文件备份到外部硬盘 我正在使用:列出子文件夹的VB.net备份程序,vb.net,copy,backup,subdirectory,Vb.net,Copy,Backup,Subdirectory,第一次贴在这里,所以请温柔一点 我正在使用Visual Studio Express 2010 我正在尝试创建一个备份程序,每晚将特定文件夹和文件备份到外部硬盘 我正在使用: Dim dirs As List(Of String) = New List(Of String)(Directory.EnumerateDirectories(dirPath)) 要获取本地驱动器上的目录列表,请执行以下操作: For Each folder In dirs If Not Directory.
Dim dirs As List(Of String) = New List(Of String)(Directory.EnumerateDirectories(dirPath))
要获取本地驱动器上的目录列表,请执行以下操作:
For Each folder In dirs
If Not Directory.Exists(FdriveDirName) Then
My.Computer.FileSystem.CopyDirectory(CdriveDirName, FdriveDirName, True)
End if
要循环浏览所有目录,请执行以下操作:
For Each folder In dirs
If Not Directory.Exists(FdriveDirName) Then
My.Computer.FileSystem.CopyDirectory(CdriveDirName, FdriveDirName, True)
End if
这对于第一个副本很好。我的问题是,当我在根目录的文件夹中创建一个新文件夹时,例如,安装一个新程序会在c中的程序文件中创建一个新文件夹:在我第二次运行程序时,不会复制这个新文件夹
我可以看出这背后的原因是EnumerateDirectory函数只列出文件夹层次结构中较低一级的文件夹
我显然需要列出所有文件夹和子文件夹,但即使用GetDirectory替换EnumerateDirectory也帮不了我
有人有什么想法吗 你需要在树上散步。您需要创建一个递归函数来遍历目录的子目录 关键是:
Private Sub RecurseDirectories(ByVal di As DirectoryInfo)
Try
For Each d In di.GetDirectories()
'Do stuff with the d (directory) here.
RecurseDirectories(d) 'get sub directories of this directory
Next
Catch
End Try
End Sub
这可能会让您进入递归算法的搜索领域 因此,您可以创建一个函数,在处理当前级别的文件夹之前,可以对每个文件夹递归调用该函数:
Public Sub ProcessFolders(ByVal inDirectoryPath As String)
For Each subFolderPath In System.IO.Directory.GetDirectories(inDirectoryPath)
ProcessFolders(subFolderPath)
'build FdriveDirName
If Not System.IO.Directory.Exists(FdriveDirName) Then
My.Computer.FileSystem.CopyDirectory(subFolderPath, FdriveDirName, True)
End If
Next
End Sub