Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Please any one help me doing this
i have a folder by name Documents in bin of my project and i have create another folder in any of the drive by name Backup ..
when i click on button the complete files from Documents must copy to Backup folder

Thanks in Advance
Posted

1 solution

C#
DirectoryInfo src = new DirectoryInfo(@"E:\Test\Dir1");
DirectoryInfo dest = new DirectoryInfo(@"C:\Dir2");
CopyDirectory(src, dest);


static void CopyDirectory(DirectoryInfo source, DirectoryInfo destination)
{
            if (!destination.Exists)
            {
                destination.Create();
            }

            // Copy all files.
            FileInfo[] files = source.GetFiles();
            foreach (FileInfo file in files)
            {
                file.CopyTo(Path.Combine(destination.FullName,
                    file.Name));
            }

            // Process subdirectories.
            DirectoryInfo[] dirs = source.GetDirectories();
            foreach (DirectoryInfo dir in dirs)
            {
                // Get destination directory.
                string destinationDir = Path.Combine(destination.FullName, dir.Name);

                // Call CopyDirectory() recursively.
                CopyDirectory(dir, new DirectoryInfo(destinationDir));
            }
}


change directories according to your requirements
 
Share this answer
 
v3
Comments
Richard MacCutchan 20-May-13 7:29am    
Please format your code.
Volynsky Alex 20-May-13 8:14am    
Nice!
navin ks 20-May-13 8:27am    
@alex thanks for edit. :)
Volynsky Alex 20-May-13 8:45am    
:)))you're welcome navin ks!
9,937,550 members 21-May-13 2:27am    
but it will not check for duplicate files while copying

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