serkan sendur 821 Postaholic Banned

this is not a question, but an application to compare two text files line by line. i attach the project to this post, i also attach two sample text files to compare. these two text files contain the names of the .wav files that comes ready with devices, HandHeld Dolphin 7900 and MC 70.

Form1.cs :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace TextComparer
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}
		private string fileName = string.Empty;
		private void button1_Click(object sender, EventArgs e)
		{
			if (openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				fileName = openFileDialog1.FileName;
				if (((Button)sender).Name == "button1")
				{
					label1.Text = Path.GetFileName(fileName);
					LoadListView(listView1);
				}
				else
				{
					label2.Text = Path.GetFileName(fileName);
					LoadListView(listView2);
				}
			}
		}

		
		private void LoadListView(ListView lv)
		{
			TextReader tr = new StreamReader(fileName);
			string input = null;
			while ((input = tr.ReadLine()) != null)
				lv.Items.Add(input);
		}

		private void btnCompare_Click(object sender, EventArgs e)
		{
			// i will make the comparison based on the first listbox
			// missing lines will be red, added lines to the second listbox will be green
			foreach(ListViewItem lvi in listView1.Items)
			{
				if(CheckItemExists(lvi,listView2)==false)
				{
					lvi.ForeColor = Color.Red;
				}
			}
			foreach (ListViewItem lvi in listView2.Items)
			{
				if (CheckItemExists(lvi, listView1) == false)
				{
					lvi.ForeColor = Color.Green;
				}
			}
		}
		private bool CheckItemExists(ListViewItem lvi, ListView lv)
		{
			bool itemExists = false;
			foreach (ListViewItem li in lv.Items)
			{
				if (li.Text == lvi.Text)
				{
					itemExists = true;
					break;
				}
			}
			return itemExists;
		}
	}
}

Form1.Designer.cs :

namespace TextComparer
{
	partial class Form1
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.btnCompare = new System.Windows.Forms.Button();
			this.listView1 = new System.Windows.Forms.ListView();
			this.listView2 = new System.Windows.Forms.ListView();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(290, 29);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(109, 23);
			this.button1.TabIndex = 2;
			this.button1.Text = "Load Text File";
			this.button1.UseVisualStyleBackColor = true;
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(706, 29);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(99, 23);
			this.button2.TabIndex = 3;
			this.button2.Text = "Load Text File";
			this.button2.UseVisualStyleBackColor = true;
			this.button2.Click += new System.EventHandler(this.button1_Click);
			// 
			// openFileDialog1
			// 
			this.openFileDialog1.FileName = "openFileDialog1";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(99, 29);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(154, 23);
			this.label1.TabIndex = 4;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(526, 29);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(151, 23);
			this.label2.TabIndex = 5;
			// 
			// btnCompare
			// 
			this.btnCompare.Location = new System.Drawing.Point(405, 156);
			this.btnCompare.Name = "btnCompare";
			this.btnCompare.Size = new System.Drawing.Size(64, 23);
			this.btnCompare.TabIndex = 6;
			this.btnCompare.Text = "Compare";
			this.btnCompare.UseVisualStyleBackColor = true;
			this.btnCompare.Click += new System.EventHandler(this.btnCompare_Click);
			// 
			// listView1
			// 
			this.listView1.Location = new System.Drawing.Point(49, 58);
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(350, 488);
			this.listView1.TabIndex = 7;
			this.listView1.UseCompatibleStateImageBehavior = false;
			this.listView1.View = System.Windows.Forms.View.List;
			// 
			// listView2
			// 
			this.listView2.Location = new System.Drawing.Point(475, 58);
			this.listView2.Name = "listView2";
			this.listView2.Size = new System.Drawing.Size(349, 488);
			this.listView2.TabIndex = 8;
			this.listView2.UseCompatibleStateImageBehavior = false;
			this.listView2.View = System.Windows.Forms.View.List;
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(852, 602);
			this.Controls.Add(this.listView2);
			this.Controls.Add(this.listView1);
			this.Controls.Add(this.btnCompare);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}

		#endregion

		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button btnCompare;
		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.ListView listView2;
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.