基于WPF实现简单的文件夹比较工具.docx
第
基于WPF实现简单的文件夹比较工具
文件比较平常都是用BeyondCompare,可以说离不开的神器,特别是针对代码比较这块,确实挺好用的。
不过BeyondCompare平常拿它主要是用来做代码比较,用来做一些大批量的二进制文件比较,其实有点不是很方便。
于是造轮子,重新写了一个简单的文件夹比较的小工具。
平常主要是拿来做一些Nuget包的比对,应用包版本的比较。
文件夹比较逻辑,采用迭代比较的方式:
usingCgdataBase;
usingFolderCompare.Models;
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Collections.ObjectModel;
usingSystem.Diagnostics;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
namespaceFolderCompare.Helpers
publicstaticclassCompareHelper
publicstaticvoidCompareDirectory(CgDirectoryInfopath1,CgDirectoryInfopath2)
if(path1.Children.IsNullOrEmpty())
if(path2.Children.IsNullOrEmpty())
path1.Result=ECompareResult.匹配;
path2.Result=ECompareResult.匹配;
else
path1.Result=ECompareResult.空;
path2.Result=ECompareResult.孤立;
SetCompareResult(path2,ECompareResult.匹配);
return;
if(path2.Children.IsNullOrEmpty())
path1.Result=ECompareResult.孤立;
path2.Result=ECompareResult.空;
SetCompareResult(path1,ECompareResult.匹配);
return;
vardirList=newListstring
varfileList=newListstring
dirList.AddRange(path1.Children.Where(s=s.IsDirectory).Select(s=s.Name));
dirList.AddRange(path2.Children.Where(s=s.IsDirectory).Select(s=s.Name));
fileList.AddRange(path1.Children.Where(s=!s.IsDirectory).Select(s=s.Name));
fileList.AddRange(path2.Children.Where(s=!s.IsDirectory).Select(s=s.Name));
varindex=0;
if(dirList.HadItems())
varitems=dirList.Distinct().ToList();
items.Sort();
foreach(variteminitems)
vardir1=path1.Children.OfTypeCgDirectoryInfo().SingleOrDefault(s=s.Name==item);
if(dir1==null)
dir1=newCgDirectoryInfo();
dir1.Result=ECompareResult.空;
path1.Children.Insert(index,dir1);
vardir2=path2.Children.OfTypeCgDirectoryInfo().Single(s=s.Name==item);
dir2.Result=ECompareResult.孤立;
varold=path2.Children.IndexOf(dir2);
path2.C