Private Sub BackupButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackupButton.Click
Dim sourcePath As String
sourcePath = Application.StartupPath & "\SavedFiles\"
Dim destPath As String
FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop
If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
destPath = FolderBrowserDialog1.SelectedPath
End If
Dim dir As New DirectoryInfo(sourcePath)
Dim infos As FileSystemInfo() = dir.GetFileSystemInfos
BackupFiles(infos)
End Sub
Private sourcePath As String
Private destPath As String
Private Sub BackupFiles(ByVal FSInfo() As FileSystemInfo)
Dim i As FileSystemInfo
For Each i In FSInfo
If TypeOf i Is DirectoryInfo Then
'i is a Directory
Dim sourceDir As String = i.FullName.ToString
'Matching dest-filespec
Dim destDir As String = Replace(sourceDir, sourcePath, destPath)
'If Dir in Backup-Destination not exists, copy Dir with SubDirs and Files
If Not My.Computer.FileSystem.DirectoryExists(destDir) Then
My.Application.DoEvents()
My.Computer.FileSystem.CopyDirectory(sourceDir, destDir, True)
Continue For
Else
Dim dInfo As DirectoryInfo = CType(i, DirectoryInfo)
'Start the loop again for existing Dirs to check for new Subdirs and Files that must be backuped
BackupFiles(dInfo.GetFileSystemInfos)
End If
ElseIf TypeOf i Is FileInfo Then
Dim sourceFile As String = i.FullName.ToString
Dim destFile As String = Replace(sourceFile, sourcePath, destPath)
If Not File.Exists(destFile) Then
My.Application.DoEvents()
File.Copy(sourceFile, destFile, True)
Else
If File.GetLastWriteTime(sourceFile) > File.GetLastWriteTime(destFile) Then
My.Application.DoEvents()
File.Copy(sourceFile, destFile, True)
End If
End If
End If
Next
End Sub
But when I run the program, it works, but it does not copy the entire folder in the application startup location.. Any ideas how I can copy that folder that is in the same location as my program to a user selected location?

New Topic/Question
Reply




MultiQuote








|