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

推荐订阅源

U
Unit 42
C
Cybersecurity and Infrastructure Security Agency CISA
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Know Your Adversary
Know Your Adversary
S
Securelist
I
Intezer
AWS News Blog
AWS News Blog
L
LINUX DO - 热门话题
P
Privacy International News Feed
Recent Announcements
Recent Announcements
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
博客园 - 聂微东
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
The GitHub Blog
The GitHub Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Schneier on Security
Schneier on Security
N
Netflix TechBlog - Medium
爱范儿
爱范儿
B
Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
CERT Recently Published Vulnerability Notes
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
Blog — PlanetScale
Blog — PlanetScale
WordPress大学
WordPress大学
S
Secure Thoughts
K
Kaspersky official blog
N
News | PayPal Newsroom
O
OpenAI News
Last Week in AI
Last Week in AI
C
Check Point Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cyberwarzone
Cyberwarzone
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Tor Project blog
大猫的无限游戏
大猫的无限游戏
Vercel News
Vercel News
D
Docker
Hugging Face - Blog
Hugging Face - Blog
T
Threat Research - Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
The Register - Security
The Register - Security
博客园 - 司徒正美
Martin Fowler
Martin Fowler
人人都是产品经理
人人都是产品经理
P
Palo Alto Networks Blog

博客园 - 爱吃糖豆的猪

Manjaro Samba 设置共享文件夹供Windows访问 【自学Python系列】Python 基础 内部数据结构之列表和元组 【自学Python系列】Python 基础 (字符串,整数,注释) 控制台I/O显示格式化的结果 【超详细教程】使用Windows Live Writer 2012和Office Word 2013 发布文章到博客园全面总结 如何在Directory.GetFiles()方法中设置多个格式呢? C#操作符??和?: ETL 解析 简单通用的访问CVS的方法 一些概念的随笔(转) 3种思维培养有决策力的孩子 是夫妻就一起把它戒了吧! 一个10年SEO工作者的35个SEO经验 可以让你少奋斗10年的工作经验 如何在一年内拥有十年的工作经验(值得你反复读5遍以上) TreeView 递归选择父节点和子节点 文件操作 C# 中的 ConfigurationManager类引用方法 添加Word,Excel等dll时如何操作。
异步拷贝文件
爱吃糖豆的猪 · 2017-03-22 · via 博客园 - 爱吃糖豆的猪
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace 同步文件
{
    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;
        }
 
 
        public SyncResult CopySync(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  ;
                }
                //递归调用目录同步函数,实现嵌套目录一次性全同步
                CopySync(sourcedir   "\\"   kvp.Key, destdir   "\\"   kvp.Key);
            }
            //取得源目录下所有文件的列表
            string[] SFiles = Directory.GetFiles(sourcedir);
            //string[] DFiles = Directory.GetFiles(destdir);
            //------比较两目录中的文件信息(本层目录)--------------
            foreach (string sfilename in SFiles)
            {
                FileInfo fi = new FileInfo(sfilename);
                if (fi.LastWriteTime > DateTime.Now.AddDays(-3))
                {
                    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;
        }
    }
}