Arsalan’s Musings

Musings on Software…

Comparing text files (C# Solution)

without comments

Once upon a time, I had to write a quick and dirty WinForm application to compare two text files line by line. I had about a day to do that so I ended up with some interesting pieces of code using the ForEach extension method passing in an anonymous delegate. It was a lot of fun.

The main two methods are below.

        /// <summary>
        /// Perform actual comparison of the two files and modify text as needed.
        /// </summary>
        private void PerformComparison()
        {
            // Read all lines in both files
            this.FirstFileLines.AddRange(File.ReadAllLines(this.FirstFileName));
            this.SecondFileLines.AddRange(File.ReadAllLines(this.SecondFileName));

            // Compare each line adding a flag
            // storing it in the second file data structure
            int maxLength = (this.FirstFileLines.Count &gt; this.SecondFileLines.Count) ?
                this.FirstFileLines.Count : this.SecondFileLines.Count;
            for (int i = 0; i &lt; maxLength; i++)
            {
                if (this.FirstFileLines.Count &lt; i + 1)
                {
                    this.SecondFileLines[i] += &quot; ";
                }
                else if (this.SecondFileLines.Count &lt; i + 1)
                {
                    this.FirstFileLines[i] += &quot; ";
                }
                else if (this.FirstFileLines[i].Equals(this.SecondFileLines[i]))
                {
                    this.SecondFileLines[i] += " ";
                }
                else
                {
                    // lines are different, so point it out
                    this.SecondFileLines[i] += " ";
                }
            }

        }

        /// <summary>
        /// Display the two files in their respective rich text boxes.
        /// </summary>
        private void DisplayFiles()
        {
            // display files in UI container
            this.richTextBoxLeft.Multiline = true;
            this.richTextBoxRight.Multiline = true;

            string firstLine = String.Empty;
            string secondLine = String.Empty;
            StringBuilder combinedString = new StringBuilder();

            // clear text boxes before writing to them
            this.richTextBoxLeft.Text = String.Empty;
            this.richTextBoxRight.Text = String.Empty;

            this.FirstFileLines.ForEach(delegate (string line)
            {
                combinedString.AppendLine(line);
                firstLine = combinedString.ToString();
            });
            combinedString = new StringBuilder();
            this.SecondFileLines.ForEach(delegate(string line)
            {
                combinedString.AppendLine(line);
                secondLine = combinedString.ToString();
            });

            this.richTextBoxLeft.Text = firstLine;
            this.richTextBoxRight.Text = secondLine;

            // clear out first and second line collections
            this.FirstFileLines.Clear();
            this.SecondFileLines.Clear();
        }

Written by Arsalan

July 6, 2009 at 6:22 pm

Leave a Reply