I am trying to copy an entire folder and all of its contents that is in the same location as my program that is running to a user selected location. I started off by using a SaveFileDialog, but now I am working with a FolderBrowserDialog. Here is some code:
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
Dim sourceDir As String = i.FullName.ToString
Dim destDir As String = Replace(sourceDir, sourcePath, destPath)
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)
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?