比较Windows应用程序中的两个文本文件

时间:2012-03-29 17:11:07

标签: c# asp.net winforms

我有两个文本框,用户可以在其中输入要比较的文本文件的路径。当他们进入路径时,我正在执行以下操作,通过逐行读取并最终将它们保存在字符串中来获取每个文件的全部内容。到目前为止它正在工作,但我需要从file2获取前10个字符并查看file1中是否存在这10个字符(如果存在)然后增加计数器,否则从file2转到接下来的10个字符并比较在file1中查看。它应该一直持续到file2结束。

 private void btnCompare_Click(object sender, EventArgs e)
        {
            string FilePath1 = txtFile1.Text;
            string FilePath2 = txtFile2.Text;
            string CompleteStringToCompare = "";
            string CompleteStringToCompareWith = "";
            int counter = 0;



            //Read First file
            if (!File.Exists(FilePath1))
            {
                Console.WriteLine("{0} does not exist.", FilePath1);
                return;
            }
            using (StreamReader sr1 = File.OpenText(FilePath1))
            {
                string input1;
                while ((input1 = sr1.ReadLine()) != null)
                {
                    CompleteStringToCompare += input1;
                }
            }

            //Read Second file
            if (!File.Exists(FilePath2))
            {
                Console.WriteLine("{0} does not exist.", FilePath2);
                return;
            }
            using (StreamReader sr2 = File.OpenText(FilePath2))
            {

                string input2;
                while ((input2 = sr2.ReadLine()) != null)
                {
                    CompleteStringToCompareWith += input2; 
                }

            }

没关系,我确实编写了我需要的其余代码。这是:

            int length = int.Parse(txtNumberOfChar.Text);  //Which is 10 in my case
            int j = 1;
            string temp = "";
            if (CompleteStringToCompareWith != "")
            {
                int totalSubStrings = CompleteStringToCompareWith.Length / length;
                for (int i = 1; i <= totalSubStrings; i++)
                {
                    if (i == totalSubStrings)
                        temp = CompleteStringToCompareWith.Substring(j, CompleteStringToCompareWith.Length - j);
                    else
                        temp = CompleteStringToCompareWith.Substring(j, length);

                    if (CompleteStringToCompare.Contains(temp))
                        counter++;
                    j = j + length;
                }

                lblMessage.Text = "Total Matches "+ counter;
            }

1 个答案:

答案 0 :(得分:1)

int length = int.Parse(txtNumberOfChar.Text);  //Which is 10 in my case
            int j = 1;
            string temp = "";
            if (CompleteStringToCompareWith != "")
            {
                int totalSubStrings = CompleteStringToCompareWith.Length / length;
                for (int i = 1; i <= totalSubStrings; i++)
                {
                    if (i == totalSubStrings)
                        temp = CompleteStringToCompareWith.Substring(j, CompleteStringToCompareWith.Length - j);
                    else
                        temp = CompleteStringToCompareWith.Substring(j, length);

                    if (CompleteStringToCompare.Contains(temp))
                        counter++;
                    j = j + length;
                }

                lblMessage.Text = "Total Matches "+ counter;
            }