In this how-to tutorial, we explain to you the step-by-step procedure to compare Text files in C#. This manual uses one of the best document comparison APIs for comparing the two text files. Further, this guide provides the sample code to show you the implementation of C# compare two Text files functionality. Here are the detailed instructions as well as sample code for comparing the two plain text files.
Steps to Compare Text Files in C#
- Setup GroupDocs.Comparison for .NET package from the NuGet website in the .NET application for comparing Text files
- Add a reference to the GroupDocs.Comparison namespace to compare two Text documents
- Load the source Text file by creating an instance of the Comparer class
- Use Add method of the Comparer class for loading the target Text document
- Call the Compare method of the Comparer class for saving the resultant Text file to the disk
The above steps allow you to easily develop the C# compare two Text files and show differences capability. You just need to write a few lines of code that contains the API calls of the document comparison library. You can also perform these instructions on any operating system like MS Windows, Linux, and Mac OS without installing additional third-party software.
Code to Compare Text Files in C#
using System; | |
using GroupDocs.Comparison; | |
namespace CompareTextFilesInCSharp | |
{ | |
class Program | |
{ | |
public static void Main(string[] args) // Main function to compare Text files using C# | |
{ | |
// Remove the watermark in output Text document by adding license | |
string licensePath = "GroupDocs.Comparison.lic"; | |
GroupDocs.Comparison.License lic = new GroupDocs.Comparison.License(); | |
lic.SetLicense(licensePath); | |
using (Comparer comparer = new Comparer("source.txt")) | |
{ | |
comparer.Add("target.txt"); | |
comparer.Compare("result.txt"); | |
} | |
Console.WriteLine("Done"); | |
} | |
} | |
} |
The above example shows the implementation of compare two Text files and highlight the differences in C# application. This sample takes two text files for comparison and saves the result in the output text document. However, you can also use this code for comparing multiple plain text files by repeating step 4. Moreover, you can quickly perform document comparison for various other document formats using the document converter APIs.
We have discussed the detailed procedures to develop the C# compare text files capability. Recently, we published an article on comparing PDF files in C#, have a look at how to Compare PDF Files using C# guide for more information.