Click here to Skip to main content
15,921,577 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
VB
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

VB
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?
Posted
Comments
#realJSOP 11-Aug-11 15:59pm    
What do you mean by "it works, but it doesn't copy the entire folder"? What doesn't copy? Folders, Files?
NY Andrew 11-Aug-11 16:02pm    
It compiles without any errors and the program itself runs smoothly, but does not copy the file when the button is pressed. There is a folder called "SavedFiles" in the same location as the executable, and inside the "SavedFiles" folder has .txts, etc. My code is not copying those files for some reason..

1 solution

Never mind. I messed around with it for another 3 hours and successfully got it working.
 
Share this answer
 
Comments
Maciej Los 14-Aug-11 15:37pm    
If you find a solution, use "Improve question" to inform that you have found it. Then you should close the discussion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900