A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

转自;http://www.jb51.net/article/45705.htm
这篇文章主要介绍了使用c#同步两个子目录文件的方法,大家参考使用吧

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace AutoSync
  7. {
  8. public class NewDirectory
  9. {
  10. public static Dictionary<string,string> GetDirectories(string dirname)
  11. {
  12. Dictionary<string, string> dirs = new Dictionary<string, string>();
  13. string[] strDirs = Directory.GetDirectories(dirname);
  14. foreach (string str in strDirs)
  15. {
  16. string[] result = str.Split('\\');
  17. dirs.Add(result[result.Length-1], result[result.Length-1]);
  18. }
  19. return dirs;
  20. }
  21. }
  22. }
  23. <DIV class=blockcode>
  24. <BLOCKQUOTE>
  25. using System;
  26. using System.Collections.Generic;
  27. using System.IO;
  28. using System.Text;
  29. namespace AutoSync
  30. {
  31. enum SyncResult
  32. {
  33. Success,SourceDirNotExists,DestDirNotExists
  34. }
  35. class FloderSync
  36. {
  37. public int CreateDirCount = 0;
  38. public int CopyFileCount = 0;
  39. public List<string> Log = new List<string>();
  40. private void AddLog(string logtext)
  41. {
  42. if (Log.Count < 1000)
  43. Log.Add(System.DateTime.Now.ToString() + logtext);
  44. else if (Log.Count == 1000)
  45. Log.Add(System.DateTime.Now.ToString() + " 达到日志上限,不再追加");
  46. }
  47. public SyncResult StartSync(string sourcedir, string destdir)
  48. {
  49. //传入目录名保护
  50. sourcedir = sourcedir.Trim();
  51. destdir = destdir.Trim();
  52. //保证目录最后一个字符不是斜杠
  53. if (sourcedir[sourcedir.Length - 1] == '\\')
  54. sourcedir = sourcedir.Remove(sourcedir.Length - 1);
  55. if (destdir[destdir.Length - 1] == '\\')
  56. destdir = destdir.Remove(destdir.Length - 1);
  57. //判断目录是否存在
  58. if (!Directory.Exists(sourcedir)) return SyncResult.SourceDirNotExists;
  59. if (!Directory.Exists(destdir)) return SyncResult.DestDirNotExists;
  60. //获取源、目的目录内的目录信息
  61. Dictionary<string, string> SDirInfo = new Dictionary<string, string>();
  62. Dictionary<string, string> DDirInfo = new Dictionary<string, string>();
  63. Dictionary<string, string> aa = new Dictionary<string, string>();
  64. SDirInfo = NewDirectory.GetDirectories(sourcedir);//获取源目录的目录信息
  65. DDirInfo = NewDirectory.GetDirectories(destdir);//获取目标目录的目录信息
  66. //
  67. // 开始同步两个目录,但只先同步源目录信息
  68. //------比较两目录中的子目录信息---------------------
  69. foreach (KeyValuePair<string, string> kvp in SDirInfo) //在查找有无源目录存在而在目标目录中不存在的目录
  70. {
  71. if(!DDirInfo.ContainsKey(kvp.Key)) //如果目标目录中不存在目录,则马上建立
  72. {
  73. string dirname=destdir + "\\" + kvp.Key;
  74. Directory.CreateDirectory(dirname);
  75. AddLog(" 创建目录:" + dirname);

  76. CreateDirCount++;
  77. }
  78. //递归调用目录同步函数,实现嵌套目录一次性全同步
  79. StartSync(sourcedir + "\\" + kvp.Key, destdir + "\\" + kvp.Key);
  80. }
  81. //取得源目录下所有文件的列表
  82. string[] SFiles = Directory.GetFiles(sourcedir);
  83. //string[] DFiles = Directory.GetFiles(destdir);
  84. //------比较两目录中的文件信息(本层目录)--------------
  85. foreach (string sfilename in SFiles)
  86. {
  87. string dfilename = destdir+"\\"+Path.GetFileName(sfilename);
  88. if (File.Exists(dfilename)) //如果目的目录中已经存在同名文件,则比较其最后修改时间,取最新的为准
  89. {
  90. //取得源、目的目录中同名文件的文件信息
  91. FileInfo sfi = new FileInfo(sfilename);
  92. FileInfo dfi = new FileInfo(dfilename);
  93. //如果源文件大于目的文件修改时间5秒以上才拷贝覆盖过去,主要是考虑到操作系统的一些差异,对于修改时间相同而文件大小不同的文件则暂不处理
  94. if (sfi.LastWriteTime > dfi.LastWriteTime.AddSeconds(5))
  95. {
  96. //拷贝源目录下的同名文件到目的目录
  97. File.Copy(sfilename, dfilename, true);
  98. AddLog(" 覆盖文件:" + dfilename);
  99. CopyFileCount++;
  100. }
  101. }
  102. else //如果目的目录中不存在同名文件,则拷贝过去
  103. {
  104. //拷贝源目录下的同名文件到目的目录
  105. File.Copy(sfilename, dfilename, true);
  106. AddLog(" 拷贝文件:" + dfilename);
  107. CopyFileCount++;
  108. }
  109. }
  110. return SyncResult.Success;
  111. }
  112. }
  113. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马