C# 快速比较两个文件内容是否相等

 static bool CompareFileContent(string file1, string file2)
        {
            // 自定义每次比较的数组大小,自行取决大小
            const int BYTES_TO_READ = 1024;
            using FileStream fs1 = File.Open(file1, FileMode.Open);
            using FileStream fs2 = File.Open(file2, FileMode.Open);
            byte[] one = new byte[BYTES_TO_READ];
            byte[] two = new byte[BYTES_TO_READ];
            bool result = true;
            while (true)
            {
                int len1 = fs1.Read(one);
                int len2 = fs2.Read(two);
                unsafe
                {
                    fixed (byte* ptr1 = one, ptr2 = two)
                    {
                        for (int i = 0; i < BYTES_TO_READ; i++)
                        {
                            if (*(ptr1 + i) != *(ptr2 + i))
                            {
                                result=false;
                                break;
                            }
                        }
                    }
                }
                if (!result || len1 == 0 || len2 == 0)
                    break;
            }
            return result;
        }

你可能感兴趣的:(c#,算法)