Click here to Skip to main content
15,942,148 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have a button I use to copy a folder from one location to another.

the code looks like this:

VB
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)
        Next

        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)
            newD.Create()
            CopyFiles(New DirectoryInfo(FolderFrom.FullName & "\" & dName), newD)
        Next

    End Sub


Now I have a folder inside 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?
Posted
Comments
GregWyatt 2-Dec-11 16:54pm    
When you say "how can I set an optional location to copy the folder to at runtime?" Do you mean that this optional location should also be inside of the project folder or to just a generic folder on the machine?
Sergey Alexandrovich Kryukov 2-Dec-11 19:50pm    
What "project folder"? There is not such thing, because this is run time. Please see my solution.
--SA
GregWyatt 2-Dec-11 17:58pm    
Molands, can you explain more about what your overall objective is? I have a few solutions in mind, but they are dependent upon ultimately what your application will be, how you want it used, etc...
molands 2-Dec-11 18:20pm    
I have a folder called exc2 in my project, and I need to be able to copy this folder to an optional location at runtime.
Sergey Alexandrovich Kryukov 2-Dec-11 19:49pm    
Why doing all that? Thinks are really much simpler.
--SA

1 solution

First of all, you are talking about run-time. During run-time, there is no such thing as "project", there are no "inside my project" and anything like that. The problem is simple: you have a directory referenced by its path name and a new location for it. All your code is also not needed. It's done by only one call: to the static method System.IO.Directory.Move, nothing else.

See http://msdn.microsoft.com/en-us/library/system.io.directory.move.aspx[^].

—SA
 
Share this answer
 

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