Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the easiest way to find out if two files are different programmatically?

Tags:

.net

What's the easiest way to find out if two text files are different programmatically? Given two files I just need to know whether they are different or not. This is for a quick tool to help with a particularly nasty merge (switched languages from VB to C# in one branch (yay!) and made many changes in the other), it won't be going into production.

Possible solutions:

  1. Hash both files and compare the hash
  2. Pull the files in and just do a string compare
  3. Call out to an external diff tool (unfortunately Winmerge doesn't have a CLI for this)

If possible ignoring white space would be awesome but I don't care that much about it. The main thing is this it needs to be quick and easy.

I'm using .Net 3.5sp1 by the way. Thanks for any ideas or pointers.

like image 518
Bryan Anderson Avatar asked Jan 29 '10 16:01

Bryan Anderson


2 Answers

There is an article in the Microsoft Knowledge Base, hope it helps. They compare the bytes to see whether two files are different - How to create a File-Compare function in Visual C#

like image 59
Steffen Avatar answered Sep 23 '22 02:09

Steffen


The fastest way to do that is comparing byte-to-byte of the files loaded on a stream. Hashing both files will take too long for large files, string compare too, external tools too.

Comparing byte-to-byte will be the best for you, as it will only reach the EOF of the files when both are identical.

If you do hash compare, string compare or external tools you'll have to go through the entire files all the times you compare, comparing byte-to-byte will do it only in case they're identical.

like image 26
Tufo Avatar answered Sep 22 '22 02:09

Tufo