This Article is for those developers who want to take backup of their working directory files on a daily basis without doing it manually.
Purpose of this article:
The main purpose of this article is to show you how you can take daily backup of your working directory, automatically. You don't have to worry if you forgot to take backup of your files, the system will do it for you, and without losing any changes. All you need to do is, just setup the automatic backup once.
So, let’s start.
Step 1
I have created a Console application and written the code for copying all the files and directories from Source Directory to Destination Directory, along with the tracking of your task log, either failed or successful, in a text file.
So, let’s start.
Step 1
I have created a Console application and written the code for copying all the files and directories from Source Directory to Destination Directory, along with the tracking of your task log, either failed or successful, in a text file.
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- namespace TakeBackupDaily
- {
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- string Source = @"D:\Source";
- string Destination = @"D:\Destination";
- CopyAllFiles(Source, Destination);
- }
- catch (Exception)
- {
- throw;
- }
- CreateTextFileLog("Task Completed successfully.");
- }
- private static void CopyAllFiles(string Source, string Destination)
- {
- try
- {
- // Get the subdirectories for the specified directory.
- DirectoryInfo dir = new DirectoryInfo(Source);
- DirectoryInfo[] dirs = dir.GetDirectories();
- if (!dir.Exists)
- {
- CreateTextFileLog("Source directory does not exist or could not be found: "
- + Source);
- }
- // If the destination directory doesn't exist, create it.
- if (!Directory.Exists(Destination))
- {
- Directory.CreateDirectory(Destination);
- }
- // 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(Destination, file.Name);
- file.CopyTo(temppath, true);
- }
- foreach (DirectoryInfo subdir in dirs)
- {
- string temppath = Path.Combine(Destination, subdir.Name);
- CopyAllFiles(subdir.FullName, temppath);
- }
- }
- catch (Exception ex)
- {
- CreateTextFileLog(ex.ToString());
- }
- }
- private static void CreateTextFileLog(string Message)
- {
- StreamWriter SW;
- if (!File.Exists(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "txt_" + DateTime.Now.ToString("yyyyMMdd") + ".txt")))
- {
- SW = File.CreateText(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "txt_" + DateTime.Now.ToString("yyyyMMdd") + ".txt"));
- SW.Close();
- }
- using (SW = File.AppendText(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "txt_" + DateTime.Now.ToString("yyyyMMdd") + ".txt")))
- {
- SW.WriteLine(Message);
- SW.Close();
- }
- }
- }
- }










Subhashkumar YadavPosted Dec 29, 2016, 12:05 AM
How to take all directory files copy from one server to another server?
Manav PandyaPosted Dec 23, 2016, 5:13 AM
Good to know article ma'm Priti Kumari
Jitendra GangwarPosted Oct 20, 2016, 2:13 PM
Nice Article....
Nitish SainiPosted Aug 11, 2016, 2:11 AM
Good. :)
Amatya AgyeyPosted Jul 13, 2016, 1:53 AM
Good 1 .. Keep it up :)
Vignesh ManiPosted Jul 11, 2016, 5:42 PM
Useful one
Gowtham KPosted Jul 10, 2016, 8:38 AM
Good One, Thanks for sharing:)
RakeshPosted Jul 10, 2016, 1:51 AM
Good share
Guest UserPosted Jul 9, 2016, 9:12 PM
The other way could be by creating a .BAT or .PS file where you write a script to do that and run via Task Scheduler
Guest UserPosted Jul 9, 2016, 9:12 PM
You implemented your way to copy and paste directory via .Net code, which is good too.
sai babuPosted Jul 9, 2016, 8:56 PM
Good one Priti
kalu singh raoPosted Jul 9, 2016, 8:44 PM
Nice...