19,502
社区成员
发帖
与我相关
我的任务
分享
bytes = new byte[1];
rand.NextBytes(bytes);
newFile.Append(bytes[0], 2000000 + rand.Next(1000000, 2000000));
//ComputeHash from firstFile and get a byte[].
//System.Security.Cryptography.HashAlgorithm
FileStream fs1 = File.Open(firstFile);
String hash1 = String.Empty;
foreach (byte b in HashAlgorithm.ComputeHash(fs1)) hash1 += b.ToString("x2").ToLower();
//copy from firstFile to secondFile
fInfo.CopyTo(secondFile, true);
//ComputeHash from secondFile and get a byte[].
FileStream fs2 = File.Open(secondFile);
String hash = String.Empty;
foreach (byte b in HashAlgorithm.ComputeHash(fs2)) hash2 += b.ToString("x2").ToLower();
//compare two hash
if (!hash2.Equals(hash1))
{
//fail
}
private bool FileCompare(string file1Path, string file2Path)
{
//if two file are point to one file. it is same
if(file1Path == file2Path)
{
return true;
}
int file1byte = 0;
int file2byte = 0;
using(FileStream fs1 = new FileStream(file1Path, FileMode.Open),
fs2 = new FileStream(file2Path, FileMode.Open))
{
if(fs1.Length != fs2.Length)
{
fs1.Close();
fs2.Close();
return false;
}
do
{
//read each byte from each file
file1byte = fs1.ReadByte();
file2byte = fs2.ReadByte();
}
while ((file1byte == file2byte) && (file1byte != -1));
fs1.Close();
fs2.Close();
return ((file1byte - file2byte) == 0);
}
}