基于WPF實(shí)現(xiàn)簡(jiǎn)單的文件夾比較工具
文件比較平常都是用Beyond Compare,可以說(shuō)離不開的神器,特別是針對(duì)代碼比較這塊,確實(shí)挺好用的。
不過Beyond Compare平常拿它主要是用來(lái)做代碼比較,用來(lái)做一些大批量的二進(jìn)制文件比較,其實(shí)有點(diǎn)不是很方便。
于是造輪子,重新寫了一個(gè)簡(jiǎn)單的文件夾比較的小工具。
平常主要是拿來(lái)做一些Nuget包的比對(duì),應(yīng)用包版本的比較。

文件夾比較邏輯,采用迭代比較的方式:
using CgdataBase;
using FolderCompare.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FolderCompare.Helpers
{
public static class CompareHelper
{
public static void CompareDirectory(CgDirectoryInfo path1, CgDirectoryInfo path2)
{
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;
}
var dirList = new List<string>();
var fileList = new List<string>();
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));
var index = 0;
if (dirList.HadItems())
{
var items = dirList.Distinct().ToList();
items.Sort();
foreach (var item in items)
{
var dir1 = path1.Children.OfType<CgDirectoryInfo>().SingleOrDefault(s => s.Name == item);
if (dir1 == null)
{
dir1 = new CgDirectoryInfo();
dir1.Result = ECompareResult.空;
path1.Children.Insert(index, dir1);
var dir2 = path2.Children.OfType<CgDirectoryInfo>().Single(s => s.Name == item);
dir2.Result = ECompareResult.孤立;
var old = path2.Children.IndexOf(dir2);
path2.Children.Move(old, index);
SetCompareResult(dir2, ECompareResult.匹配);
}
else
{
var dir2 = path2.Children.OfType<CgDirectoryInfo>().SingleOrDefault(s => s.Name == item);
if (dir2 == null)
{
dir2 = new CgDirectoryInfo();
dir2.Result = ECompareResult.空;
path2.Children.Insert(index, dir2);
dir1.Result = ECompareResult.孤立;
var old = path1.Children.IndexOf(dir1);
path1.Children.Move(old, index);
SetCompareResult(dir1, ECompareResult.匹配);
}
else
{
CompareDirectory(dir1, dir2);
var old = path1.Children.IndexOf(dir1);
path1.Children.Move(old, index);
old = path2.Children.IndexOf(dir2);
path2.Children.Move(old, index);
}
dir2.Index = index;
}
dir1.Index = index;
index++;
}
}
if (fileList.HadItems())
{
var items = fileList.Distinct().ToList();
items.Sort();
foreach (var item in items)
{
var file1 = path1.Children.OfType<CgFileInfo>().SingleOrDefault(s => s.Name == item);
if (file1 == null)
{
file1 = new CgFileInfo();
file1.Result = ECompareResult.空;
path1.Children.Insert(index, file1);
var file2 = path2.Children.OfType<CgFileInfo>().Single(s => s.Name == item);
file2.Result = ECompareResult.孤立;
var old = path2.Children.IndexOf(file2);
path2.Children.Move(old, index);
}
else
{
var file2 = path2.Children.OfType<CgFileInfo>().SingleOrDefault(s => s.Name == item);
if (file2 == null)
{
file2 = new CgFileInfo();
file2.Result = ECompareResult.空;
path2.Children.Insert(index, file2);
file1.Result = ECompareResult.孤立;
var old = path1.Children.IndexOf(file1);
path1.Children.Move(old, index);
}
else
{
CompareFile(file1, file2);
var old = path1.Children.IndexOf(file1);
path1.Children.Move(old, index);
old = path2.Children.IndexOf(file2);
path2.Children.Move(old, index);
}
file2.Index = index;
}
file1.Index = index;
index++;
}
}
path1.Result = GetCompareResult(path1.Children);
path2.Result = GetCompareResult(path2.Children);
}
private static void CompareFile(CgFileSystemInfo info1, CgFileSystemInfo info2)
{
if (info1.Name.IsNullOrEmpty())
{
info1.Result = ECompareResult.空;
info2.Result = ECompareResult.孤立;
return;
}
if (info2.Name.IsNullOrEmpty())
{
info1.Result = ECompareResult.孤立;
info2.Result = ECompareResult.空;
return;
}
if (info1.Length == info2.Length && info1.LastWriteTime == info2.LastWriteTime)
{
info1.Result = ECompareResult.匹配;
info2.Result = ECompareResult.匹配;
}
else
{
if (info1.LastWriteTime > info2.LastWriteTime)
{
info1.Result = ECompareResult.不匹配;
info2.Result = ECompareResult.較舊的;
}
else
{
info1.Result = ECompareResult.較舊的;
info2.Result = ECompareResult.不匹配;
}
}
}
private static void SetCompareResult(CgDirectoryInfo info, ECompareResult result)
{
if (info.Children.HadItems())
{
foreach (var item in info.Children)
{
if (item is CgDirectoryInfo dir)
{
dir.Result = result;
SetCompareResult(dir, result);
}
else if (item is CgFileInfo file)
{
file.Result = result;
}
}
}
}
public static ECompareResult GetCompareResult(ObservableCollection<CgFileSystemInfo> items)
{
if (items.IsNullOrEmpty())
return ECompareResult.空;
if (items.Any(s => s.Result == ECompareResult.不匹配 || s.Result == ECompareResult.較舊的))
return ECompareResult.不匹配;
if (items.Any(s => s.Result == ECompareResult.孤立))
return ECompareResult.孤立;
return ECompareResult.匹配;
}
}
}源碼:https://gitee.com/wzwyc/FolderCompare
以上就是基于WPF實(shí)現(xiàn)簡(jiǎn)單的文件夾比較工具的詳細(xì)內(nèi)容,更多關(guān)于WPF文件夾比較的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解C#如何解決程序卡頓的問題(多線程初步學(xué)習(xí))
在編寫程序的時(shí)候,有時(shí)候難免會(huì)出現(xiàn)后臺(tái)運(yùn)行時(shí)間過長(zhǎng)的問題,這個(gè)時(shí)候就要考慮多線程的操作了,所以本文給大家介紹了C#解決程序卡頓問題的方法,需要的朋友可以參考下2024-04-04
C#中List集合使用Max()方法查找到最大值的實(shí)例
這篇文章主要介紹了C#中List集合使用Max()方法查找到最大值的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-12-12
數(shù)字金額大寫轉(zhuǎn)換器制作代碼分享(人民幣大寫轉(zhuǎn)換)
一個(gè)人民幣大寫的擴(kuò)展方法,可以做成數(shù)字金額大寫轉(zhuǎn)換器,大家參考使用吧2013-12-12
C#實(shí)現(xiàn)訪問遠(yuǎn)程硬盤的圖文教程
在現(xiàn)實(shí)場(chǎng)景中,我們經(jīng)常用到遠(yuǎn)程桌面功能,而在某些場(chǎng)景下,我們需要使用類似的遠(yuǎn)程硬盤功能,這樣能非常方便地操作對(duì)方電腦磁盤的目錄、以及傳送文件,這次我們將給出一個(gè)完整的Demo來(lái)演示遠(yuǎn)程硬盤要怎么實(shí)現(xiàn),需要的朋友可以參考下2025-05-05
C#計(jì)算輸入漢字GBK編碼后十六進(jìn)制數(shù)輸出的方法
這篇文章主要介紹了C#計(jì)算輸入漢字GBK編碼后十六進(jìn)制數(shù)輸出的方法,涉及C#編碼操作相關(guān)技巧,需要的朋友可以參考下2015-04-04
C#修改及重置電腦密碼DirectoryEntry實(shí)現(xiàn)方法
這篇文章主要介紹了C#修改及重置電腦密碼DirectoryEntry實(shí)現(xiàn)方法,實(shí)例分析了C#修改及重置電腦密碼的相關(guān)技巧,需要的朋友可以參考下2015-05-05
Unity3D游戲開發(fā)數(shù)據(jù)持久化PlayerPrefs的用法詳解
在本篇文章里小編給大家整理了關(guān)于Unity3D游戲開發(fā)之?dāng)?shù)據(jù)持久化PlayerPrefs的使用的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們參考下。2019-08-08

