Hi everyone.

I started playing around with C# development. As my first project I decided to write a program that performs a simple backup of given folders.

In essence it goes through files and folder in source directory and destination directory, and then:

if the folder in destination directory doesn't exist creates it;

if the file in destination directory doesn't exist copies it over from source directory;

if the file in destination directory exist:
1. check last write time of file in destination and source directory
2. if source file is newer than destination copy over and overwrite

at the same time progress of work on files is displayed in richTextBox, line by line using += "some text\n"

My problem is that the program only works on the main dirctory files and folders. It won't go any deeper. Example:

Source
Folder1
File1
File2
Folder2
File1
File2
File1
File2
File3

Destination
Folder1
Folder2
File1
File2
File3

How can I make it go through nested folders.

The action is fired when I click a button on the form. Below is the code i have so far

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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            label1.Visible = false;
            label2.Visible = false;
            label3.Visible = false;
            richTextBox1.Visible = false;
        }


        private void button1_Click(object sender, EventArgs e)
        {

            string originalDirectory = @"C:\Test";
            string newDir = @"F:\test";
            int dirCount = 0;
            int fileCount = 0;

            label1.Visible = true;
            richTextBox1.Visible = true;
            label1.Text = "Backup progress:";
            
            //Ensure new directory exists
            if (!Directory.Exists(newDir))
                Directory.CreateDirectory(newDir);

            DirectoryInfo oldDir = new DirectoryInfo(originalDirectory);

            //Copy files
            foreach (FileInfo file in oldDir.GetFiles())
            {
                string oldPath = file.FullName;
                string newPath = newDir + "\\" + file.Name;

                // if file doesn't exist copy
                if (!File.Exists(newPath)){
                File.Copy(oldPath, newPath);
                richTextBox1.Text += newPath + " copied.\n";
                fileCount++;
                }

                //if file exist, check last modification date. If newer overwrite
                if (File.Exists(newPath))
                {
                    DateTime oldWriteTime = File.GetLastWriteTime(oldPath);
                    DateTime newWriteTime = File.GetLastWriteTime(newPath);

                    if (oldWriteTime > newWriteTime)
                    {
                        File.Copy(oldPath, newPath, true);
                        richTextBox1.Text += "Source file " + oldPath + " is newer that the destination file. File has been overwriten\n";
                        fileCount++;
                    }
                    else
                    {
                        richTextBox1.Text += "Destination file " + newPath + " is newer that the source file. No need to backup\n";
                    }
                }

            }

            foreach(DirectoryInfo dir in oldDir.GetDirectories())
            {
                string oldDirPath = dir.FullName;
                string newDirPath = newDir + "\\" + dir.Name;
                if (!Directory.Exists(newDirPath))
                {
                    Directory.CreateDirectory(newDirPath);
                    richTextBox1.Text += newDirPath + " directory created.\n";
                    dirCount++;
                }
            }
            
            richTextBox1.Text += "\n-----------------------------\n Completed.";
            label2.Visible = true;
            label2.Text = "Directories copied: "+dirCount;
            label3.Visible = true;
            label3.Text = "Files copied: "+fileCount;
        }
    }
}

Also, Directory.GetFiles() has an overload that allows you to specify search options. It looks like this:

string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.bmp", SearchOption.AllDirectories);

Here's a link with more details:
http://www.csharp-examples.net/get-files-from-directory/
Hope this helps!

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.