C#实现简单的文件夹对比程序(续)

上一篇文章实现了可视化显示不同路径下相同文件夹内的文件和子文件夹差异,文件少时已经够用,但当有成百上千个子文件夹时,人工逐个查看文件夹差异就比较困难,因此本文实现循环查找文件夹内容差异并导出为本地Excel文件的功能。
  本文使用队列保存要差异对比的文件夹及子文件夹路径,没有使用递归,代码逻辑看着稍微有点别扭,主要代码如下:

csharp 复制代码
List<DirectoryCompareRecord> results= new List<DirectoryCompareRecord>();

Queue<string> subDirs= new Queue<string>();

string leftDir=txtLeftDir.Text.TrimEnd('\\');
string rightDir=txtRightDir.Text.TrimEnd('\\');
string curLeftDir = string.Empty;
string curRightDir = string.Empty;

List<string> subLeftDirs = new List<string>();
List<string> subRightDirs = new List<string>();
List<string> subLeftFiles = new List<string>();
List<string> subRightFiles = new List<string>();

string tmpFileName = string.Empty;
string tmpDirName = string.Empty;
string tmp = string.Empty;

FileInfo leftFile = null;
FileInfo rightFile = null;

do
{                
    curLeftDir = Path.Combine(leftDir, tmp);
    curRightDir = Path.Combine(rightDir, tmp);

    subLeftDirs.Clear();
    subRightDirs.Clear();
    subLeftFiles.Clear();
    subRightFiles.Clear();
    

    subLeftFiles.AddRange(Directory.GetFiles(curLeftDir));
    subRightFiles.AddRange(Directory.GetFiles(curRightDir));

    subLeftFiles.Sort();
    subRightFiles.Sort();

    foreach (string file in subLeftFiles)
    {
        tmpFileName = Path.GetFileName(file);
        tmpDirName = Path.Combine(curRightDir, tmpFileName);

        if (!subRightFiles.Contains(tmpDirName))
        {
            DirectoryCompareRecord record = new DirectoryCompareRecord();
            record.ObjectType = "文件";
            record.ResultType = "文件缺失";
            record.LeftDirectory = curLeftDir;
            record.LeftFile = tmpFileName;

            results.Add(record);
        }
        else
        {
            leftFile=new FileInfo(file);
            rightFile=new FileInfo(tmpDirName);

            if (leftFile.LastWriteTime != rightFile.LastWriteTime)
            {
                DirectoryCompareRecord record = new DirectoryCompareRecord();
                record.ObjectType = "文件";
                record.ResultType = "文件有差异";
                record.LeftDirectory = curLeftDir;
                record.LeftFile = tmpFileName;
                record.LeftFileLastModifyDate = leftFile.LastWriteTime.ToString();
                record.RightDirectory = curRightDir;
                record.RightFile = tmpFileName;
                record.RightFileLastModifyDate = rightFile.LastWriteTime.ToString();

                results.Add(record);
            }
            else
            {
                subRightFiles.Remove(tmpDirName);
            }                        
        }
    }

    foreach (string file in subRightFiles)
    {
        tmpFileName = Path.GetFileName(file);
        tmpDirName = Path.Combine(curLeftDir, tmpFileName);

        if (!subLeftFiles.Contains(tmpDirName))
        {
            DirectoryCompareRecord record = new DirectoryCompareRecord();
            record.ObjectType = "文件";
            record.ResultType = "文件缺失";
            record.RightDirectory = curRightDir;
            record.RightFile = tmpFileName;

            results.Add(record);
        }
    }

    subLeftDirs.AddRange(Directory.GetDirectories(curLeftDir));
    subRightDirs.AddRange(Directory.GetDirectories(curRightDir));

    subLeftDirs.Sort();
    subRightDirs.Sort();

    foreach (string dir in subLeftDirs)
    {
        tmpFileName = Path.GetFileName(dir);
        tmpDirName = Path.Combine(curRightDir, tmpFileName);

        if (!subRightDirs.Contains(tmpDirName))
        {
            DirectoryCompareRecord record = new DirectoryCompareRecord();
            record.ObjectType = "文件夹";
            record.ResultType = "文件夹缺失";
            record.LeftDirectory = curLeftDir;
            record.LeftFile = tmpFileName;

            results.Add(record);
        }
        else
        {
            subDirs.Enqueue(dir.Substring(leftDir.Length+1));
            subRightFiles.Remove(tmpDirName);                        
        }
    }

    foreach (string file in subRightDirs)
    {
        tmpFileName = Path.GetFileName(file);
        tmpDirName = Path.Combine(curLeftDir, tmpFileName);

        if (!subLeftDirs.Contains(tmpDirName))
        {
            DirectoryCompareRecord record = new DirectoryCompareRecord();
            record.ObjectType = "文件夹";
            record.ResultType = "文件夹缺失";
            record.RightDirectory = curRightDir;
            record.RightFile = tmpFileName;

            results.Add(record);
        }
    }

    if(subDirs.Count>0)
    {
        tmp=subDirs.Dequeue();
    }
    
} while (subDirs.Count >0);

对比结果导出Excel文件采用Sylvan.Data.Excel库,代码实现也比较简单,如下所示:

csharp 复制代码
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "XLSX|*.xlsx";
if (saveFileDialog.ShowDialog() != DialogResult.OK)
{
    return;
}

DbDataReader reader = ObjectDataReader.Create(results);
using (var w = ExcelDataWriter.Create(saveFileDialog.FileName))
{
    w.Write(reader);
}

最后是程序运行效果及导出文件的效果,代码地址:https://github.com/guochao2299/DirCompare

参考文献:

1\]https://blog.csdn.net/sd7o95o/article/details/142976888 \[2\]https://github.com/MarkPflug/Sylvan.Data.Excel

相关推荐
“抚琴”的人5 小时前
【机械视觉】C#+VisionPro联合编程———【六、visionPro连接工业相机设备】
c#·工业相机·visionpro·机械视觉
FAREWELL000757 小时前
C#核心学习(七)面向对象--封装(6)C#中的拓展方法与运算符重载: 让代码更“聪明”的魔法
学习·c#·面向对象·运算符重载·oop·拓展方法
CodeCraft Studio7 小时前
Excel处理控件Spire.XLS系列教程:C# 合并、或取消合并 Excel 单元格
前端·c#·excel
勘察加熊人9 小时前
forms实现连连看
c#
hvinsion9 小时前
PPT助手:一款集计时、远程控制与多屏切换于一身的PPT辅助工具
c#·powerpoint·ppt·ppt助手·ppt翻页
weixin_3077791310 小时前
使用C#实现从Hive的CREATE TABLE语句中提取分区字段名和数据类型
开发语言·数据仓库·hive·c#
时光追逐者11 小时前
在 Blazor 中使用 Chart.js 快速创建数据可视化图表
开发语言·javascript·信息可视化·c#·.net·blazor
与火星的孩子对话12 小时前
Unity3D开发AI桌面精灵/宠物系列 【三】 语音识别 ASR 技术、语音转文本多平台 - 支持科大讯飞、百度等 C# 开发
人工智能·unity·c#·游戏引擎·语音识别·宠物
response_L12 小时前
国产系统统信uos和麒麟v10在线打开word给表格赋值
java·c#·word·信创·在线编辑
MasterNeverDown12 小时前
Swagger2Md:让WebAPI文档生成变得轻松高效
c#