C#检测两个文件内容是否相同
不知道为什么对Excel 2010 xlsx后缀的文件没有效果,求解!
对其他文件有效,如.txt,.csv
1 using System; 2 using System.Security.Cryptography; 3 using System.IO; 4 5 namespace CheckFile 6 { 7 class Program 8 { 9 static void Main(string[] args) 10 { 11 string filePath1 = @"D:/1.txt"; 12 string filePath2 = @"D:/2.txt"; 13 bool result = CheckFile(filePath1, filePath2); 14 Console.WriteLine(result.ToString()); 15 Console.ReadKey(); 16 17 } 18 /// <summary> 19 /// Check File 20 /// </summary> 21 /// <param name="filePath1"></param> 22 /// <param name="filePath2"></param> 23 /// <returns></returns> 24 public static bool CheckFile(string filePath1, string filePath2) 25 { 26 using (HashAlgorithm hash = HashAlgorithm.Create()) 27 { 28 using (FileStream file1 = new FileStream(filePath1, FileMode.Open, FileAccess.Read) 29 , file2 = new FileStream(filePath2, FileMode.Open, FileAccess.Read)) 30 { 31 byte[] hashByte1 = hash.ComputeHash(file1); 32 byte[] hashByte2 = hash.ComputeHash(file2); 33 string str1 = BitConverter.ToString(hashByte1); 34 string str2 = BitConverter.ToString(hashByte2); 35 return (str1 == str2); 36 } 37 } 38 } 39 } 40 }
转载请注明文章来自:程序猴(http://www.chengxuhou.com/)
posted on 2013-12-26 17:13 程序猴chengxuhou.com 阅读(1053) 评论(0) 编辑 收藏 举报