天天看点

C#实现每隔一段时间自动备份数据

using System;
using System.IO;
using System.Windows.Forms;

namespace  TEST
{
    public class BackupHelper
    {

        string sourcePath = null;
        string destPath = null;

    
        /// <summary>
        /// 启动备份程序
        /// </summary>
        /// <param name="startpath">原路径</param>
        /// <param name="endpath">目标路径</param>
        /// <param name="Time">备份时间间隔(单位:秒)</param>
        public void StartMission(string startpath, string endpath, int Time)
        {
            sourcePath = startpath;
            destPath = endpath;
            int time = Time * 60 * 1000;


            System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
            timer.Interval = time;
            timer.Tick += Timer_Tick;
            timer.Start();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {

            System.Threading.Thread thread = new System.Threading.Thread(() => {
                try
                {
                    DirectoryInfo soure = new DirectoryInfo(sourcePath);
                    DirectoryInfo dest = new DirectoryInfo(destPath);

                    if (!dest.Exists)
                    {
                        Directory.CreateDirectory(destPath);
                        CopyFolders(sourcePath, destPath, true);
                    }
                    else
                    {
                        CopyFolders(sourcePath, destPath, true);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            });
            thread.IsBackground = true;
            thread.Start();
        }


        /// <summary>
        /// 复制文件夹
        /// </summary>
        /// <param name="sourceFilePath">原文件夹地址</param>
        /// <param name="destinationFilePath">目标文件夹地址</param>
        /// <param name="overwrite">是否允许覆盖</param>
        private void CopyFolders(string sourceFilePath, string destinationFilePath, bool overwrite )
        {
            try
            {
                DirectoryInfo startDir = new DirectoryInfo(sourceFilePath);
                DirectoryInfo endDir = new DirectoryInfo(destinationFilePath);
                if (!endDir.Exists)
                {
                    Directory.CreateDirectory(destinationFilePath);
                }

                foreach (var item in startDir.GetFiles())
                {
                    File.Copy(item.FullName, endDir + "\\" + item.Name, overwrite);
                }

                ///递归调用///
                foreach (var item in startDir.GetDirectories())
                {
                    string new_sourcefilepath = item.FullName;
                    string new_destinationfilepath = destinationFilePath + "\\" + item.Name;
                    CopyFolders(new_sourcefilepath, new_destinationfilepath , overwrite);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}