Copy directories including sub folders and files inside the main directory
In this code snippet, we will take a look how you to copy the directories including sub folders and files inside the main directory from any selected directory. In the bellow mentioned code we just need select the source and destination and data will be copied to the destination location from the source specified.
Steps to work with code-snippet
:
1. Create one form
2. Insert two textboxes and three buttons inside the form
TextBox Names :
SourceTextBox
DestinationTextBox
Button Names :
SourceButton
DestinationButton
ButtonCopyData
3. Here are the required namesaces
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
4. Use following code for click event of source button
private void SourceButton_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog fd = new FolderBrowserDialog())
{
if (DialogResult.OK == fd.ShowDialog())
{
SourceTextBox.Text = fd.SelectedPath;
}
}
}
5. Bellow code is for destination button
private void DestinationButton_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog fd = new FolderBrowserDialog())
{
if (DialogResult.OK == fd.ShowDialog())
{
DestinationTextBox.Text = fd.SelectedPath;
}
}
}
6. Here is our copy directory function
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
try
{
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, false);
}
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
MessageBox.Show("Backup done!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
7. Write code on Copy Data button as mentioned below
private void ButtonCopyData_Click(object sender, EventArgs e)
{
if(SourceTextBox.Text != null && DestinationTextBox.Text != null)
{
DirectoryCopy(SourceTextBox.Text, DestinationTextBox.Text, true);
}
else
{
MessageBox.Show("Please specify the source and destination","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
// DirectoryCopy function will copy the directory including sub directories and it's content to the new location specified in the destination textbox
}
Here, we are done!
Use above code-snippet and enjoy!