0306数据备份整理-【备份文件】C#代码

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;

namespace BackUp
{
    /// <summary>
    /// 对本地备份操作
    /// </summary>
    public class BackUp
    {
        public string sourcefolder;//源路径
        public string destinationfolder;//目标路径
        public string tempfolder;//临时目录
        public string filetype;//备份文件格式
        public DateTime lasttime;//最后时间
        public string logpath;
        //构造函数初始化备份类
        public BackUp(string _sourcefolder, string _destinationfolder, string _filetype,DateTime _lasttime,string _logpath)
        {
            sourcefolder = _sourcefolder;
            destinationfolder = _destinationfolder;
            filetype = _filetype;
            lasttime = _lasttime;
            logpath = _logpath;
        }

        //开始备份,文件比较
        public void StartBackUp()
        {
            DirectoryInfo sourcedir = new DirectoryInfo(sourcefolder);
            FileInfo[] sourcefiles = sourcedir.GetFiles();
            DirectoryInfo destinationdir = new DirectoryInfo(destinationfolder);
            FileInfo[] destinationfiles = destinationdir.GetFiles();
            ArrayList list = new ArrayList();
            foreach (FileInfo item in destinationfiles)
            {
                list.Add(item.Name);
            }
            string[] type = filetype.Split(';');
            foreach (FileInfo sf in sourcefiles)
            {
                foreach (var t in type)
                {
                    if (sf.Name.LastIndexOf(t) != -1 && !list.Contains(sf.Name))
                    {                      
                        sf.CopyTo(destinationfolder + sf.Name);
                        break;
                    }
                }

            }
        }

        //根据最后时间 备份
        public DateTime BackUpByTime()
        {
            //记录最大时间
            DateTime MaxTime = lasttime;         
            string[] type = filetype.Split(';');
            //日志清单创建 
            Logs logs = new Logs(logpath);
            CopyAllFile(sourcefolder, ref MaxTime, ref logs, ref type,destinationfolder);         
            //关闭读写流
            logs.CloseStream();
            return MaxTime;
           
            ////目录拷贝
            //DirectoryInfo[] childdirs = sourcedir.GetDirectories();
         

        }

        public void CopyAllFile(string parentfolder, ref DateTime MaxTime, ref Logs logs, ref string[] type,string temppath )
        {
            DirectoryInfo sourcedir = new DirectoryInfo(parentfolder);        
            FileInfo[] sourcefiles = sourcedir.GetFiles();
            if (!Directory.Exists(temppath))
            {
                try
                {
                    Directory.CreateDirectory(temppath);
                }
                catch(Exception ex)
                {
                    logs.LogFileList(temppath, " 创建文件夹失败", ex);
                }
            }
             //文件拷贝
            foreach (FileInfo sf in sourcefiles)
            {
                //如果文件创建日期大于最后日期
                if (sf.CreationTime.Ticks - lasttime.Ticks > 10000011)
                {
                    foreach (var t in type)
                    {
                        if (sf.Name.LastIndexOf(t) != -1)
                        {
                            try
                            {
                                sf.CopyTo(temppath + sf.Name);
                                logs.LogFileList(sf.FullName, "备份成功", null);
                                //改变时间
                                if (sf.CreationTime.CompareTo(MaxTime) > 0)
                                    MaxTime = sf.CreationTime;
                                break;
                            }
                            catch (Exception ex)
                            {
                                logs.LogFileList(sf.FullName, "备份失败,已经存在同样名字的文件", ex);
                                //改变时间
                                if (sf.CreationTime.CompareTo(MaxTime) > 0)
                                    MaxTime = sf.CreationTime;
                                break;
                            }

                        }
                    }
                }
            }
            //目录拷贝
            DirectoryInfo[] childdirs = sourcedir.GetDirectories();        
            foreach (var dir in childdirs)
            {             
                temppath = destinationfolder + dir.FullName.Replace(sourcefolder,"")+@"\";
                CopyAllFile(dir.FullName, ref MaxTime, ref logs, ref type,temppath);
            }        
                   

        }
        public void FileCopy(ref FileInfo[] sourcefiles,ref DateTime MaxTime,ref Logs logs,ref string[] type)
        {


            //文件拷贝
            foreach (FileInfo sf in sourcefiles)
            {
                //如果文件创建日期大于最后日期
                if (sf.CreationTime.CompareTo(lasttime) > 0)
                {
                    foreach (var t in type)
                    {
                        if (sf.Name.LastIndexOf(t) != -1)
                        {
                            try
                            {
                                sf.CopyTo(destinationfolder + sf.Name);
                                logs.LogFileList(sf.FullName, "备份成功", null);
                                //改变时间
                                if (sf.CreationTime.CompareTo(MaxTime) > 0)
                                    MaxTime = sf.CreationTime;
                                break;
                            }
                            catch (Exception ex)
                            {
                                logs.LogFileList(sf.FullName, "备份失败", ex);
                            }

                        }
                    }


                }

            }
        }
    }
}

posted @ 2010-03-06 11:22  会游泳dě鱼  阅读(265)  评论(0编辑  收藏  举报