在这里,我描述了一个示例场景:
“FileA-Database.txt”包含以下名称:
KB200
KB300
KB400
“FileB-Slave.txt”包含以下名称:
KB600
KB200
KB400
KB700
我想将“FileA-Database.txt”与“FileB-Slave.txt”进行比较,并让缺失值自动填充到“FileA-Database.txt”文件中,我还需要在文本中显示缺失值名为“Results.txt”的文件。
代码需要与 C# (framework 4.0+) 兼容!
我需要一种简单的方法,但我的方法并不能完全按照我想要的方式工作:
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
}