我有这段代码可以比较两个文本文件并将差异写入日志文件,但由于某种原因,log.txt 文件有时是空白的,即使是用 * 开头的某些行进行测试,这些也不总是写我有吗完成写作后保存文本文件,尽管这并不能解释为什么有时它会起作用,但任何帮助都会很棒
private void compare()
{
string FilePath = @"c:\snapshot\1.txt";
string Filepath2 = @"c:\snapshot\2.txt";
int counter = 0;
string line;
string line2;
var dir = "c:\\snapshot\\log.txt";
using (FileStream fs = File.Create(dir))
{
fs.Dispose();
}
StreamWriter dest = new StreamWriter(dir);
if (File.Exists(FilePath) & File.Exists(Filepath2))
{
// Read the file and display it line by line.
using (var file = File.OpenText(FilePath))
using (var file2 = File.OpenText(Filepath2))
{
while (((line = file.ReadLine()) != null & (line2 = file2.ReadLine()) != null))
{
if (line.Contains("*"))
{
dest.WriteLine(line2);
}
else if (!line.Contains(line2))
{
dest.WriteLine(line2);
}
counter++;
}
}
}
dest.Close();
}