65.9K
CodeProject is changing. Read more.
Home

Backup Utility in C#

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.79/5 (14 votes)

Aug 5, 2003

viewsIcon

138351

downloadIcon

9842

A utility to backup directories and stick a datestamp on the folder, and remove folders older than N days, where N is configurable.

Sample Image - backup.gif

Introduction

During a development project, I realized I needed something to backup my solutions every night, and be tagged with a date stamp. I looked around the Net for some DOS utilities but none really fitted the bill as far as what I was looking for. Eventually, I decided to just code it. Turned out to be super simple in C#.

Goal

My goal was to backup my solution from a source folder (e.g. C:\Data) to a backup folder (e.g. C:\Backup\Data_080403). I also wanted to only keep N number of days of backups and not let my backup folder grow infinitely. So, for instance, I wanted to keep 30 days worth of backups at most. So, whenever, any files older than 30 days were discovered, I wanted the system to delete them automatically.

Solution

I used the System.IO's Directory and DirectoryInfo classes to create folders, delete folders, and wrote a method to recursively copy folders from one location to the next. I utilized the DirectoryInfo class to figure out how old a given folder was, in order to delete it.