转自;http://www.jb51.net/article/45705.htm
这篇文章主要介绍了使用c#同步两个子目录文件的方法,大家参考使用吧
- 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;
- }
- }
- }
- <DIV class=blockcode>
- <BLOCKQUOTE>
- 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;
- }
- }
- }
复制代码
|
|