File comparison

Asked

Viewed 715 times

4

I need to compare files from different directories and show the differences. But I don’t want to list the name of the files, I just want you to make 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 make a comparison of the two directories and of all the files and bring the differences.

  • 2

    What have you done? What is your specific question?

  • 1

    Maybe Winmerge or file comparison software will help you.

1 answer

5


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

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

I put in the Github for future reference.

Some improvements can be made, such as loading the file on demand. Without loading byte per byte, which would be too slow, the ideal is to have a buffer of at least 4096 bytes.

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

If performance is very important maybe the use of pointers can help. As well as avoid the LINQ that has a small overhead. Again, just testing to be sure.

Some extra checks can be done if needed.

To make a difference between them is much more complicated and it is difficult to make right. Google has already made one diff before using known algorithms. It has a few tricks since it is a port.

There are other questions here that show how to scan a directory, just adapt to the desired algorithm:

  • Bigown, I would like the comparison to scan the directory, thus comparing all the files within the directory. To better understand, I have an official Solution in a directory and a local Solution, I want to compare the two and show the differences of the files . Cs that have in these directories. The idea is to show if the local Solution is equal to the official Solution.

  • 1

    So your question is too wide, it should be closed, I’ve already helped something. When you ask a question, you have to be specific, put what you’ve done, where your doubt is. When you do not give details, you give room for any answer. Asking to do everything for you is not cool. I am giving the basics. If you have specific problems, post new questions.

  • @Guilhermenass this is more like versioning.

  • Guys, my question can be solved with what they talked about using comparison software (Beyond, winmerge), but wanted to make the comparison in c#.

  • @Guilhermenass, following the tip of the bigown and because I found the idea interesting, I thought you could create a public project on github.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.