Unity - C#比较两个文件是否相同

前言:我的情况是需要检测目标目录文件在发生变动时,将其拷贝至另一个目录

Hash比较法

复制代码
	static bool CompareFileHash(string sourcePath, string targetPath)
	{
        // 目标文件不存在,则返回不相同
		if (!File.Exists(targetPath))
			return false;

		string getHash(string filePath)
		{
			using (var md5 = MD5.Create())
			using (var stream = File.OpenRead(filePath))
			{
				byte[] hashBytes = md5.ComputeHash(stream);
				return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
			}
		}
		var hash1 = getHash(sourcePath);
		var hash2 = getHash(targetPath);
		Debug.Log($"{sourcePath}|{hash1}\n{targetPath}|{hash2}");
		return hash1 == hash2;
	}

增加文件长度比较

复制代码
    public static bool CompareFile(string sourcePath, string targetPath)
    {
        if (!File.Exists(targetPath))
            return false;
            
        FileInfo sourceInfo = new FileInfo(sourcePath);
        FileInfo targetInfo = new FileInfo(targetPath);
        
        if (sourceInfo.Length != targetInfo.Length)
            return false;
        
        return CompareFileHash(sourcePath,targetPath);
    }
相关推荐
棪燊9 小时前
Unity的Game视图在Scale放大后无法拖动
unity·游戏引擎
weixin_4239950011 小时前
unity 团结开发小游戏,加载AssetBundles
unity·游戏引擎
cyr___12 小时前
Unity教程(二十七)技能系统 黑洞技能(下)黑洞状态
学习·游戏·unity·游戏引擎
齐鲁大虾12 小时前
新人编程语言选择指南
javascript·c++·python·c#
加号313 小时前
【C#】 WebAPI 接口设计与实现指南
开发语言·c#
张老师带你学13 小时前
Unity 科幻武器系列
科技·游戏·unity·模型·游戏美术
unicrom_深圳市由你创科技13 小时前
上位机开发常用的语言 / 框架有哪些?
c++·python·c#
平行云14 小时前
虚拟直播混合式2D/3D应用程序实时云渲染推流解决方案
linux·unity·云原生·ue5·图形渲染·实时云渲染·像素流送
cyr___15 小时前
Unity教程(二十六)技能系统 黑洞技能(上)基础实现
学习·游戏·unity·游戏引擎
xiaoshuaishuai815 小时前
C# ZLibrary数字资源分发
开发语言·windows·c#