C#文件相同性判断

  在进行开发时,对文件进行上传和下载是较为普遍的行为,为了防止在文件操作过程中,出现同一文件多次操作,需要对文件进行相同性比较:

    1.获取文件的绝对路径,针对window程序和web程序都可使用:

  

 /// <summary>
        /// 获取文件的绝对路径,针对window程序和web程序都可使用
        /// </summary>
        /// <param name="relativePath">相对路径地址</param>
        /// <returns>绝对路径地址</returns>
        public static string GetAbsolutePath(string relativePath)
        {
            if (string.IsNullOrEmpty(relativePath))
            {
                throw new ArgumentNullException("参数relativePath空异常!");
            }
            relativePath = relativePath.Replace("/", "\\");
            if (relativePath[0] == '\\')
            {
                relativePath=relativePath.Remove(0, 1);
            }
            //判断是Web程序还是window程序
            if (HttpContext.Current != null)
            {
                return Path.Combine(HttpRuntime.AppDomainAppPath, relativePath);
            }
            else
            {
                return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath);
            }
        }

   2.获取文件的绝对路径,针对window程序和web程序都可使用:

   

 /// <summary>
        /// 获取文件的绝对路径,针对window程序和web程序都可使用
        /// </summary>
        /// <param name="relativePath">相对路径地址</param>
        /// <returns>绝对路径地址</returns>
        public static string GetRootPath()
        {
            //判断是Web程序还是window程序
            if (HttpContext.Current != null)
            {
                return HttpRuntime.AppDomainAppPath;
            }
            else
            {
                return AppDomain.CurrentDomain.BaseDirectory;
            }
        }

   3.通过文件Hash 比较两个文件内容是否相同:

    

  /// <summary>
        /// 通过文件Hash 比较两个文件内容是否相同
        /// </summary>
        /// <param name="filePath1">文件1地址</param>
        /// <param name="filePath2">文件2地址</param>
        /// <returns></returns>
        public static bool isValidFileContent(string filePath1, string filePath2)
        {
            //创建一个哈希算法对象 
            using (HashAlgorithm hash = HashAlgorithm.Create())
            {
                using (FileStream file1 = new FileStream(filePath1, FileMode.Open), file2 = new FileStream(filePath2, FileMode.Open))
                {
                    byte[] hashByte1 = hash.ComputeHash(file1);//哈希算法根据文本得到哈希码的字节数组 
                    byte[] hashByte2 = hash.ComputeHash(file2);
                    string str1 = BitConverter.ToString(hashByte1);//将字节数组装换为字符串 
                    string str2 = BitConverter.ToString(hashByte2);
                    return (str1 == str2);//比较哈希码 
                }
            }
        }

  4.计算文件的hash值 用于比较两个文件是否相同:

  

 /// <summary>
        /// 计算文件的hash值 用于比较两个文件是否相同
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>文件hash值</returns>
        public static string GetFileHash(string filePath)
        {
            //创建一个哈希算法对象 
            using (HashAlgorithm hash = HashAlgorithm.Create())
            {
                using (FileStream file = new FileStream(filePath, FileMode.Open))
                {
                    //哈希算法根据文本得到哈希码的字节数组 
                    byte[] hashByte= hash.ComputeHash(file);
                    //将字节数组装换为字符串  
                    return BitConverter.ToString(hashByte);
                }
            }
        }

 

转载于:https://www.cnblogs.com/pengze0902/p/5944101.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
判断 JSON 文件数据是否相同项,主要需要考虑两个方面:首先是如何读取 JSON 文件数据,其次是如何判断文件数据中是否相同的项。下面是一个简单的 C# 实现方法: ```csharp using System; using System.IO; using System.Collections.Generic; using Newtonsoft.Json; class Program { static void Main(string[] args) { string jsonFile = "data.json"; // JSON 文件路径 string jsonData = File.ReadAllText(jsonFile); // 读取 JSON 文件数据 List<Dictionary<string, string>> data = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(jsonData); // 解析 JSON 数据为字典列表 HashSet<string> set = new HashSet<string>(); // 用 HashSet 存储所有出现过的值 foreach (Dictionary<string, string> dict in data) { foreach (string value in dict.Values) { if (!set.Add(value)) // 如果值已经存在于 HashSet 中,则说明有相同项 { Console.WriteLine("JSON 数据中有相同项:" + value); } } } } } ``` 这个程序首先读取 JSON 文件数据,然后使用 Newtonsoft.Json 库中的 JsonConvert.DeserializeObject 方法将 JSON 数据解析为一个包含多个字典的列表。接着,程序使用 HashSet 存储所有出现过的值,并在遍历字典列表时检查每个值是否已经存在于 HashSet 中。如果值已经存在,则说明数据中有相同项,程序会输出相应的提示信息。 需要注意的是,这个实现方法只是一种简单的示例,如果 JSON 数据的格式和结构比较复杂,可能需要进行更加复杂的处理和判断

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值