我有一个用 .net 2.0 编写的程序,我需要比较两个文本文件。我试过下面的代码
Dim fileA As String
Dim fileB As String
Dim fileypath As String = (Environment.GetEnvironmentVariable("APPDATA") & "\ARLS\")
Dim sReaderA As New IO.StreamReader(New IO.FileStream(fileypath & "orig.dat", IO.FileMode.Open))
Dim sReaderB As New IO.StreamReader(New IO.FileStream(fileypath & "comp.dat", IO.FileMode.Open))
fileA = sReaderA.ReadToEnd
fileB = sReaderB.ReadToEnd
sReaderA.Close()
sReaderB.Close()
If fileA.CompareTo(fileB) = -1 Then
MessageBox.Show(fileB.Replace(fileA, "")) '/// show the words in fileB which differ from fileA.
End If
If fileB.CompareTo(fileA) = -1 Then
MessageBox.Show(fileA.Replace(fileB, "")) '/// show the words in fileB which differ from fileB.
End If
它的工作原理是它只会显示是否将某些内容添加到行尾。如果在中间添加或删除任何内容,它将显示整个文本文件。有任何想法吗。
<--EDIT-->
所以我通过创建两个列表框,将文本文件转储到每个列表框,然后将列表框与以下代码进行比较来实现它。For i As Integer = 0 To ListBox2.Items.Count - 1
If ListBox1.Items.Contains(ListBox2.Items(i)) Then
Else
LogPrint(ListBox2.Items(i))
End If
Next
For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox2.Items.Contains(ListBox1.Items(i)) Then
Else
LogPrint(ListBox1.Items(i))
End If
Next
ListBox2.Items.Clear()
ListBox1.Items.Clear()
这给了我差异,但这似乎还有很长的路要走。任何人都知道是否有更好的方法可以在不使用列表框的情况下做到这一点?