Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

c# - Returning records that have been added or removed after two file comparison

I am comparing two files and one of the files might have added or removed items. I am checking to see if there are differences between the two files and if so then what are those differences whether records have been added or removed. I would like to return those records (added or removed)

With what i have it tells me whether the file had items removed or added but it is not returning the the items that have been added or removed. Any help to what i am missing would be appreciated.

   foreach (ExcelRow rowA in fileA.excelRows)
    {
        if (!fileB.ContainsHash(rowA.rowHash))
        {
            MessageBox.Show("Files are NOT the same. Data was REMOVED.
" + rowA.ToString());
        }

    }

    foreach (ExcelRow rowB in fileB.excelRows)
    {
         if (!fileA.ContainsHash(rowB.rowHash))
         {
              MessageBox.Show("Row added" + rowB.ToString());
         }
    }

public List<ExcelRow> excelRows = new List<ExcelRow>();

        public bool ContainsHash(byte[] hashToLook)
        {
            bool found;
            found = false;
            foreach (ExcelRow eRow in excelRows)
            {
                found = EqualHash(eRow.rowHash, hashToLook);

                if (found)
                {
                    break;
                }
            }

            return found;
        }

        public static bool EqualHash(byte[] hashA, byte[] hashB)
        {
            bool bEqual ;
            int i ;

            bEqual  = false;
            if (hashA.Length == hashB.Length)
            {

                i = 0;
                while ((i < hashA.Length) && (hashA[i] == hashB[i]))
                {
                    i++ ;
                }
                if (i == hashA.Length)
                {
                    bEqual = true;
                }
            }
            return bEqual ;
        }

Reading Files:

public ExcelInfo ReadExcel(OpenFileDialog openFileDialog)
        {
            var _excelFile = new ExcelQueryFactory(openFileDialog.FileName);
            var _info = from c in _excelFile.WorksheetNoHeader() select c;

            ExcelRow excelRow;
            ExcelInfo resp;

            resp = new ExcelInfo();

            foreach (var item in _info)
            {
                excelRow = new ExcelRow();
                excelRow.lstCells.Add(item.ElementAt(0));
                excelRow.lstCells.Add(item.ElementAt(1));
                excelRow.lstCells.Add(item.ElementAt(2));
                excelRow.lstCells.Add(item.ElementAt(3));
                excelRow.lstCells.Add(item.ElementAt(4));
                excelRow.lstCells.Add(item.ElementAt(5));
                excelRow.lstCells.Add(item.ElementAt(6));
                excelRow.lstCells.Add(item.ElementAt(7));
                excelRow.lstCells.Add(item.ElementAt(8));
                excelRow.lstCells.Add(item.ElementAt(9));
                excelRow.lstCells.Add(item.ElementAt(10));
                excelRow.lstCells.Add(item.ElementAt(11));
                excelRow.lstCells.Add(item.ElementAt(12));

                excelRow.CalculateHash();
                resp.excelRows.Add(excelRow);
            }
            return resp;
        }

Calculate Hash:

public void CalculateHash()
        {
            byte[] rowBytes;
            byte[] cellBytes;
            int pos;
            int numRowBytes;

            numRowBytes = 0;
            foreach (string cellText in lstCells)
            {
                numRowBytes += NumBytes(cellText);
            }

            //Allocate space to calculate the HASH of a single row

            rowBytes = new byte[numRowBytes];
            pos = 0;

            //Concatenate the cellText of each row into a single byte array
            foreach (string cellText in lstCells)
            {
                cellBytes = GetBytes(cellText);
                System.Buffer.BlockCopy(cellBytes, 0, rowBytes, pos, cellBytes.Length);
                pos = cellBytes.Length;
            }
            rowHash = new MD5CryptoServiceProvider().ComputeHash(rowBytes);
        }

WHILE DEBUGGING:

if (!fileB.ContainsHash(rowA.rowHash))

fileB contains three rows and fileA contains 4 rows.

fileB = 3, rowA = the first row in fileA and (.rowHash) is byte[16]

as i continue to ContainHash method, byte[] hashToLook = 16 - shouldn't this be rowA?

excelRows = 3 (fileB)

then EqualHash(eRow.rowHash, hashToLook) is (first row in fileA, byte[16])

am i passing in rowA wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are almost there, just add two lists to keep track of items that are in A but not in B and items that are in B but not in A:

var notInA = new List<ExcelRow>();
var notInB = new List<ExcelRow>();

Now in your code, add them to the appropriate list:

foreach (ExcelRow rowA in fileA.excelRows)
{
    if (!fileB.ContainsHash(rowA.rowHash))
    {
        MessageBox.Show("Files are NOT the same. Data was REMOVED.
" + rowA.ToString());
        notInB.Add(rowA);
    }

}

foreach (ExcelRow rowB in fileB.excelRows)
{
     if (!fileA.ContainsHash(rowB.rowHash))
     {
          MessageBox.Show("Row added" + rowB.ToString());
          notInA.Add(rowB);
     }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...