惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - xujh

ASP.NET中突破上传4M文件的限制 【转】一种在SQLServer中实现Sequence的高效方法 DevExpress的ASPxGridView中实现Master-Detail数据动态绑定 【转】浅谈:国内软件公司为何无法做大做强? - xujh - 博客园 一个正则表达式测试(只可输入中文、字母和数字) - xujh - 博客园 IE6、7下如何设置页面内部件高度和窗口等高(heihgt:100%) HP大中华区总裁孙振耀退休感言 C#的一个双缓冲显示的例子 【ASP.NET】防止ASP.NET按钮多次提交的办法 【ASP.NET】FCKeditor 2.6 + Asp.Net 设置 【ASP.NET】FreeTextBox的使用方法 在VS2005下配置手机设备仿真器访问局域网 [Delphi] 截屏存盘 C# 画圆角矩形 有意思的加菲猫语录 [Delphi]用Delphi7访问.NET 2.0的WebService 转贴[ASP.NET]基于Forms认证的WebService应用 ASP.NET优秀博客搜集 [转贴]DataGrid中的高级ToolTip
C# 同步两个子目录文件
xujh · 2012-12-12 · via 博客园 - xujh

Posted on 2012-12-12 14:21  xujh  阅读(414)  评论()    收藏  举报

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace AutoSync
{
    public class NewDirectory
    {
        public static Dictionary<string,string> GetDirectories(string dirname)
        {
            Dictionary<string, string> dirs = new Dictionary<string, string>();
            string[] strDirs = Directory.GetDirectories(dirname);
            foreach (string str in strDirs)
            {
                string[] result = str.Split('\\');
                dirs.Add(result[result.Length-1], result[result.Length-1]);
            }
            return dirs;
        }
    }
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace AutoSync
{
    enum SyncResult
    {
        Success,SourceDirNotExists,DestDirNotExists
    }
    class FloderSync
    {
        public int CreateDirCount = 0;
        public int CopyFileCount = 0;
        public List<string> Log = new List<string>();
        private void AddLog(string logtext)
        {
            if (Log.Count < 1000)
                Log.Add(System.DateTime.Now.ToString() + logtext);
            else if (Log.Count == 1000)
                Log.Add(System.DateTime.Now.ToString() + "  达到日志上限,不再追加");
        }
        public SyncResult StartSync(string sourcedir, string destdir)
        {
            //传入目录名保护
            sourcedir = sourcedir.Trim();
            destdir = destdir.Trim();
            //保证目录最后一个字符不是斜杠
            if (sourcedir[sourcedir.Length - 1] == '\\')
                sourcedir = sourcedir.Remove(sourcedir.Length - 1);
            if (destdir[destdir.Length - 1] == '\\')
                destdir = destdir.Remove(destdir.Length - 1);
            //判断目录是否存在
            if (!Directory.Exists(sourcedir)) return SyncResult.SourceDirNotExists;
            if (!Directory.Exists(destdir)) return SyncResult.DestDirNotExists;

            //获取源、目的目录内的目录信息
            Dictionary<string, string> SDirInfo = new Dictionary<string, string>();
            Dictionary<string, string> DDirInfo = new Dictionary<string, string>();
            Dictionary<string, string> aa = new Dictionary<string, string>();
            SDirInfo = NewDirectory.GetDirectories(sourcedir);//获取源目录的目录信息
            DDirInfo = NewDirectory.GetDirectories(destdir);//获取目标目录的目录信息
            //
            //      开始同步两个目录,但只先同步源目录信息
            //------比较两目录中的子目录信息---------------------
            foreach (KeyValuePair<string, string> kvp in SDirInfo) //在查找有无源目录存在而在目标目录中不存在的目录
            {
                if(!DDirInfo.ContainsKey(kvp.Key)) //如果目标目录中不存在目录,则马上建立
                {
                    string dirname=destdir + "\\" + kvp.Key;
                    Directory.CreateDirectory(dirname);
                    AddLog("  创建目录:" + dirname);
                    
                    CreateDirCount++;
                }
                //递归调用目录同步函数,实现嵌套目录一次性全同步
                StartSync(sourcedir + "\\" + kvp.Key, destdir + "\\" + kvp.Key);
            }
            //取得源目录下所有文件的列表
            string[] SFiles = Directory.GetFiles(sourcedir);
            //string[] DFiles = Directory.GetFiles(destdir);
            //------比较两目录中的文件信息(本层目录)--------------
            foreach (string sfilename in SFiles)
            {
                string dfilename = destdir+"\\"+Path.GetFileName(sfilename);
                if (File.Exists(dfilename)) //如果目的目录中已经存在同名文件,则比较其最后修改时间,取最新的为准
                {
                    //取得源、目的目录中同名文件的文件信息
                    FileInfo sfi = new FileInfo(sfilename);
                    FileInfo dfi = new FileInfo(dfilename);
                    //如果源文件大于目的文件修改时间5秒以上才拷贝覆盖过去,主要是考虑到操作系统的一些差异,对于修改时间相同而文件大小不同的文件则暂不处理
                    if (sfi.LastWriteTime > dfi.LastWriteTime.AddSeconds(5))
                    {
                        //拷贝源目录下的同名文件到目的目录
                        File.Copy(sfilename, dfilename, true);
                        AddLog("  覆盖文件:" + dfilename);
                        CopyFileCount++;
                    }
                }
                else //如果目的目录中不存在同名文件,则拷贝过去
                {
                    //拷贝源目录下的同名文件到目的目录
                    File.Copy(sfilename, dfilename, true);
                    AddLog("  拷贝文件:" + dfilename);
                    CopyFileCount++;
                }
            }
            return SyncResult.Success;
        }
    }
}