I have a button I use to copy a folder from one location to another.

the code looks like this:

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Const FOLDER_FROM As String = "C:\Testcopy1" ' Folder to copy from 
        Const FOLDER_TO As String = "C:\Mappestruktur" 'Folder to create above folder in 
        Dim newDir As DirectoryInfo = Directory.CreateDirectory(FOLDER_TO & "\" & New DirectoryInfo(FOLDER_FROM).Name)
        CopyFiles(New DirectoryInfo(FOLDER_FROM), newDir)
    End Sub

    Private Sub CopyFiles(ByVal FolderFrom As DirectoryInfo, ByVal FolderTo As DirectoryInfo)
        Dim Files() As String = Directory.GetFiles(FolderFrom.FullName)
        Dim SF() As String = Directory.GetDirectories(FolderFrom.FullName)
        Dim Var As Integer

        For Var = Files.GetLowerBound(0) To Files.GetUpperBound(0)
            File.Copy(Files(Var), FolderTo.FullName & "\" & New FileInfo(Files(Var)).Name)

        For Var = SF.GetLowerBound(0) To SF.GetUpperBound(0)
            Dim dName As String = New DirectoryInfo(SF(Var)).Name
            Dim newD As New DirectoryInfo(FolderTo.FullName & "\" & dName)
            CopyFiles(New DirectoryInfo(FolderFrom.FullName & "\" & dName), newD)

    End Sub

Now I have a folder in my project I would like to copy to an optional location.
how do I set the "Folder from" const to a location inside my project and how can I set an optional location to copy the folder to at runtime?

Recommended Answers

All 10 Replies

First of all may I suggest that you use the built-in copy methods rather than rolling your own.


will do the copy of the entire folder just fine. That will save you some debugging. If you want to set a folder constant just do

Const srceFolder = "D:\MyFolder"

To get a user selected folder add a FolderBrowserDialog object to your project and use it as follows:

Dim destFolder As String

FolderBrowserDialog1.Description = "Select a destination folder"
FolderBrowserDialog1.ShowNewFolderButton = True

if FolderBrowserDialog1.ShowDialog = vbOK Then
    destFolder = FolderBrowserDialog1.SelectedPath
End If

This worked great, but how do I set the srceFolder to a folder inside the project?
I have a folder called exc2 in my project I want to use as the srceFolder.

If you want to browse for a source folder then just use the FolderBrowseDialog and change the title appropriately.

Dim fromFolder As String = IO.Path.Combine(My.Application.Info.DirectoryPath, "exc2")
		If IO.Directory.Exists(fromFolder) Then
			'show the FolderBrowserDialog for destination folder
		End If

I copied the folders to the debug folder and debugged the program and it worked, but when I published the program and installed it it could not find the folder.
I guess somehow the folders dous not publish with the rest of the program.
Can anyone give me a hint why? there is no property value to change ant the folders are included in my project.

set the property of the files inside that folder to
Build action: Content
Copy to output directory: copy if newer

Then your files will be copied to your build directory.

but the folderstucture is empty, do I need to have a file in all the folders for them to be copied?

then you should add a dummy file in the very last folder. else the folders won't get copied.

Ok, is there a way to add a hidden dummyfile?

not that i know about, but you can check on application startup if this file exist and delete it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.