File Comparison

4

I need to compare different directory files and show the differences. But I do not want to list the name of the files, I just want you to do a comparison of the directories and show the differences of the files. These files would be .cs .

Example:

string diretorio1 = "c:\teste"
string diretorio2 = "c:\teste2"

It will compare the two directories and all the files and bring the differences.

    
asked by anonymous 21.01.2016 / 12:07

1 answer

4

There are several ways to do this, each with its advantages. As there are no restrictions I will put what is probably the simplest form. I'm using LINQ :

var arquivo1 = File.ReadAllBytes(nomeArquivo1);
var arquivo2 = File.ReadAllBytes(nomeArquivo2);
WriteLine(arquivo1.SequenceEqual(arquivo2)); //mostra se é igual ou não

Some improvements can be made, such as uploading the file on demand. Without loading byte by byte, it would be very slow, ideally to have a buffer of at least 4096 bytes.

At the time of assembling the array , instead of using a array , it could assemble the bytes into sets of 8 and store in an array of Int64 and compare it. The comparison will be faster, but I do not know how much the algorithm as a whole would be faster, have to consider the expense with cast , the logic to assemble this, the data load that would have to be A little different. Just testing to make sure which one would be faster.

If performance is very important, pointers may help. So how to avoid LINQ that has a small overhead . Again, just testing to be sure.

Some extra checks can be made if necessary.

To make a differential between them is much more complicated and difficult to do right. Google has diff before using known algorithms. It has some gambiarras since it is a port .

You have other questions here that show you how to scan a directory, just adpate to the desired algorithm:

21.01.2016 / 12:28