Directory Copy

directoryCopy

This was a simple directory copy program I made up for our desktop group. We have some computers located in the manufacturing area that are pretty locked down so users can’t do anything other than the jobs they were designed to-do. However the guys still need the ability to do certain functions in the even of a crash. So I gave them this quick little program to help them out. It’s s simple 2 class program. They pick a source and a destination folder. They have the option of copying the sub-folders if need be. There is a status window to let the user know what’s going on with the program.

DirectoryCopy Class:


using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Directory_Copy
{
    public partial class DirectoryCopy : Form
    {
        private FolderBrowserDialog folderSelect = new FolderBrowserDialog();    //FolderBrowser Dialog object initialization.
        private Copy _myCopy; 

        public DirectoryCopy()
        {
            InitializeComponent();
            _myCopy = new Copy();    //Creating a new Copy object to use.

            ToolTip toolTip = new ToolTip();    //Setting the tool tips for the user.
            toolTip.SetToolTip(sourceDirButton, "Pick A Source Directory");
            toolTip.SetToolTip(destDirButton, "Pick A Destination Directory");
            toolTip.SetToolTip(copyButton, "Copy Files / Folders");
            toolTip.SetToolTip(exitButton, "Close Program");
        }

        private void sourceDirButton_Click(object sender, EventArgs e)
        {            
            folderSelect.Description = "Pick A Location To Copy From.";
            folderSelect.SelectedPath = @"C:\";

            DialogResult dialogResult = folderSelect.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                _myCopy.SourceFolder = folderSelect.SelectedPath;
                sourceDirTextBox.Text = _myCopy.SourceFolder;
                copyStatusListBox.Items.Add("Source Folder: " + _myCopy.SourceFolder + " Selected...");
            }
        }

        private void destDirButton_Click(object sender, EventArgs e)
        {
            folderSelect.Description = "Pick A Location To Copy To.";
            folderSelect.SelectedPath = @"C:\";

            DialogResult dialogResult = folderSelect.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                _myCopy.DestFolder = folderSelect.SelectedPath;
                destDirTextBox.Text = _myCopy.DestFolder;
                copyStatusListBox.Items.Add("Destination Folder: " + _myCopy.DestFolder + " Selected...");
            }
        }

        private void copySubDirCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            if (copySubDirCheckbox.Checked)
            {
                _myCopy.CopySubDirs = true;
                copyStatusListBox.Items.Add("Copy Subdirectories Box Checked...");
            }
            else
            {
                _myCopy.CopySubDirs = false;
                copyStatusListBox.Items.Add("Copy Subdirectories Box Unchecked...");
            }
        }

        private void copyButton_Click(object sender, EventArgs e)
        {
            if (sourceDirTextBox.Text.Length == 0)
            {
                _myCopy.MessageBox_Stop("Please Select A Source Directory", "Source Directory");
            }
            else if (destDirTextBox.Text.Length == 0)
            {
                _myCopy.MessageBox_Stop("Please Select A Destination Directory", "Destination Directory");
            }
            else
            {
                CopyDirectory(_myCopy.SourceFolder, _myCopy.DestFolder, _myCopy.CopySubDirs);
            }
        }

        private void CopyDirectory(string sourceDirName, string destDirName, bool copySubDirs)
        {
            // Get the subdirectories for the specified directory.
            DirectoryInfo dir = new DirectoryInfo(sourceDirName);
            DirectoryInfo[] dirs = dir.GetDirectories();

            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, true);

                //Updating & scrolling the copy status list box.
                copyStatusListBox.SelectedIndex = copyStatusListBox.Items.Count - 1;
                copyStatusListBox.Items.Add("Copied File: " + file.Name + " Successfully");
            }

            // 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);
                    CopyDirectory(subdir.FullName, temppath, copySubDirs);

                    //Updating & scrolling the copy status list box.
                    copyStatusListBox.SelectedIndex = copyStatusListBox.Items.Count - 1;
                    copyStatusListBox.Items.Add("Copied Folder: " + subdir.Name + " Successfully");
                }
            }
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

Copy Class


using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Directory_Copy
{
    class Copy
    {
        private string _sourceFolder = "";
        private string _destFolder = "";
        private bool copySubDirs = true;

        public string SourceFolder
        {
            get { return _sourceFolder; }
            set { _sourceFolder = value; }
        }

        public string DestFolder
        {
            get { return _destFolder; }
            set { _destFolder = value; }
        }

        public bool CopySubDirs
        {
            get { return copySubDirs; }
            set { copySubDirs = value; }
        }

        public void MessageBox_Stop(string errorDisplay, string emptyField)
        {
            MessageBox.Show(errorDisplay,
                emptyField,
                MessageBoxButtons.OK,
                MessageBoxIcon.Stop,
                MessageBoxDefaultButton.Button1);
        }
    }
}

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s