在 C# 中删除文本文件中包含另一个文件中的文本的所有行

0 c# console-application

但我想在 C# 中与此线程相同。

换句话说,我有 3 个文本文件:

  • content.txt
  • remove.txt
  • result.txt

我想删除content.txt包含任何行的所有行remove.txt并将结果输出到result.txt.

Dmi*_*nko 5

您可以尝试Linq :从行不在的位置写入result.txt所有行:content.txtremove.txt

 using System.IO;
 using System.Linq;

 ...

 HashSet<string> toRemove = new HashSet<string>(File.ReadLines(@"remove.txt"));

 File.WriteAllLines(@"result.txt", File
   .ReadLines(@"content.txt")
   .Where(line => !toRemove.Contains(line)));
Run Code Online (Sandbox Code Playgroud)