C# 逐行比较两个文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14370070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:37:40  来源:igfitidea点击:

Compare two text files line by line

c#filetextcompareline

提问by Developman

Beneath here i described a example Scenario:

下面我描述了一个示例场景:

"FileA-Database.txt" contains the following names:

“FileA-Database.txt”包含以下名称:

KB200

KB200

KB300

KB300

KB400

KB400

"FileB-Slave.txt" contains the following names:

“FileB-Slave.txt”包含以下名称:

KB600

KB600

KB200

KB200

KB400

KB400

KB700

KB700

I want to compare the "FileA-Database.txt" with "FileB-Slave.txt" and let the missing values be filled in automatically in the "FileA-Database.txt" file also i need to display the missing values in a text file called "Results.txt".

我想将“FileA-Database.txt”与“FileB-Slave.txt”进行比较,并让缺失值自动填充到“FileA-Database.txt”文件中,我还需要在文本中显示缺失值名为“Results.txt”的文件。

The code needs to be compatible with C# (framework 4.0+) please!.

代码需要与 C#(框架 4.0+)兼容!。

I need a simple approach, mine doesnt work exactly the way i want it to:

我需要一个简单的方法,我的方法并不完全按照我想要的方式工作:

    private void button_compare_Click(object sender, EventArgs e)
        {
            string fileA, fileB, fileC;
            fileA = "database-critical.txt";
            fileB = "patchlist.txt";
            fileC = "result.txt";

            string alphaFilePath = fileA;

            List<string> alphaFileContent = new List<string>();

            using (FileStream fs = new FileStream(alphaFilePath, FileMode.Open))
            using(StreamReader rdr = new StreamReader(fs))
            {
                while(!rdr.EndOfStream)
                {
                    alphaFileContent.Add(rdr.ReadLine());
                }
            }

            string betaFilePath = fileB;

            StringBuilder sb = new StringBuilder();


            using (FileStream fs = new FileStream(betaFilePath, FileMode.Open))
            using (StreamReader rdr = new StreamReader(fs))
            {
                while(! rdr.EndOfStream)
                {
                    string[] betaFileLine = rdr.ReadLine().Split(Convert.ToChar(","));

                    if (alphaFileContent.Contains(betaFileLine[0]))
                    {
                        sb.AppendLine(String.Format("{0}", betaFileLine[0]));
                    }
                }
            }

using (FileStream fs = new FileStream(fileC, FileMode.Create))
        using (StreamWriter writer = new StreamWriter(fs))
        {
            writer.Write(sb.ToString());
        }
    }

            //End
        }

采纳答案by Tommaso Belluzzo

String directory = @"C:\Whatever\";
String[] linesA = File.ReadAllLines(Path.Combine(directory, "FileA-Database.txt"));
String[] linesB = File.ReadAllLines(Path.Combine(directory, "FileB-Database.txt"));

IEnumerable<String> onlyB = linesB.Except(linesA);

File.WriteAllLines(Path.Combine(directory, "Result.txt"), onlyB);

回答by Joey

Since your question seems like you made no effort whatsoever I'll give you just a rough outline.

既然你的问题似乎你没有做任何努力,我就给你一个粗略的概述。

  1. Read both files line by line, e.g. with File.ReadAllLinesor File.ReadLines.
  2. Use LINQ's Exceptmethod.
  3. Write the results into a new file.
  1. 逐行读取两个文件,例如使用File.ReadAllLinesFile.ReadLines
  2. 使用 LINQ 的Except方法。
  3. 将结果写入新文件。