Search code examples
c#utility

Make a Backup Utility in C#


I'm thinking of a Backup Utility as my next project idea, but I don't really know where to start or how the backups should be made.

Can anyone shine some light on what methods of archiving backups and restoring etc. is done.

Thanks


Solution

  • To start with, I don't believe I've seen any namespace or classes in the .NET Framework specifically designed for backup/archive/restore activities. If anyone knows of any, well, enlighten us please!

    At its simplest, backing up files is simply copying files from one location to another. Let's say that you have source code that you would like to maintain a backup of, and it changes a bit day by day. You could create a small console app that would simply copy all the files in the target folders to an external drive, overwriting any files that had changed since the last copy operation, and adding any new files and folders to the destination. Then use a Scheduled Task to run the utility once per day at a time when the computer is not being otherwise used -- or at least when the source is not being edited.

    There are lots of methods and classes in the System.IO namespace that will make this relatively easy to do.

    Some useful classes in the IO namespace are:

    • Directory provides static methods for creating, moving and enumerating through directories and subdirecties
    • DirectoryInfo is a class exposing properties of Folders/Directories
    • File provides static methods for files
    • FileInfo is a class exposing properties of files.

    Here's a code sample that would give you an array of all the top-level folders just under the root of the C: drive:

    string[] d = Directory.GetDirectories(@"C\", "*.*", SearchOption.TopDirectoryOnly);
    DirectoryInfo[] di = new DirectoryInfo[d.Length];
    for (int x = 0; x < d.Length; x++)
    {
        di[x] = new DirectoryInfo(d[x]);
    }