首页  编辑  

比较两个文件是否相同

Tags: /C#/文件目录操作/   Date Created:

private bool SameBytes( byte [] ABuffer1 , byte [] ABuffer2)

{

    if (ABuffer1 . Length != ABuffer2 . Length) return false ;

    for ( int i = 0 ; i < ABuffer1 . Length; i ++ )

        if (ABuffer1[i] != ABuffer2[i]) return false ;

    return true ;

}

private bool SameFile( string AFileName1 , string AFileName2)

{

    FileInfo vFileInfo1 = new FileInfo (AFileName1);

    FileInfo vFileInfo2 = new FileInfo (AFileName2);

    if (vFileInfo1 . Length != vFileInfo2 . Length) return false ;

    FileStream vFileStream1 = new FileStream (AFileName1,

        FileMode . Open, FileAccess . Read, FileShare . Read);

    FileStream vFileStream2 = new FileStream (AFileName2,

        FileMode . Open, FileAccess . Read, FileShare . Read);

    byte [] vBuffer1 = new byte [ 0x1000 ];

    byte [] vBuffer2 = new byte [ 0x1000 ];

    int vReadLength1 = vFileStream1 . Read(vBuffer1, 0 , vBuffer1 . Length);

    int vReadLength2 = vFileStream2 . Read(vBuffer2, 0 , vBuffer2 . Length);

    while ((vReadLength1 > 0 ) && (vReadLength2 > 0 ))

   {

       vReadLength1 = vFileStream1 . Read(vBuffer1, 0 , vBuffer1 . Length);

       vReadLength2 = vFileStream2 . Read(vBuffer2, 0 , vBuffer2 . Length);

        if ( ! SameBytes(vBuffer1, vBuffer2)) return false ;

   }

   vFileStream1 . Close();

   vFileStream2 . Close();

    return true ;

}