Public Class Form1
Const TEST1 = "\\folder\compare\list1.txt"
Const TEST2 = "\\folder\compare\list2.txt"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare two dictionaries. The key for each will be the text from the input line up to,
'but not including the first ",". The valus for each will be the entire input line.
Dim file1 As New Dictionary(Of String, String)
Dim file2 As New Dictionary(Of String, String)
For Each line As String In System.IO.File.ReadAllLines(TEST1)
Dim part() As String = line.Split(",")
file1.Add(part(0), line)
Next
For Each line As String In System.IO.File.ReadAllLines(TEST2)
Dim part() As String = line.Split(",")
file2.Add(part(0), line)
Next
AddText("The following lines from " & TEST2 & " are also in " & TEST1)
For Each key As String In file2.Keys
If file1.ContainsKey(key) Then
AddText(file2(key))
End If
Next
End Sub
Private Sub AddText(ByVal text As String)
txtResults.Text &= text & vbCrLf
End Sub
清单1)12345
清单2)1234
结果:5不在列表1中
我想比较vb.net中的两个文本文件,以检查是否有值不在列表一中 我面临的问题是列表比较匹配的所有值,但我也想要一个不匹配的值列表。我想显示列表1中不在列表2中的值。
答案 0 :(得分:0)
在for循环中,输入else
并将值放在另一个列表中:
For Each key As String In file2.Keys
If file1.ContainsKey(key) Then
AddText(file2(key))
Else
...Add to anther list or string here
End If
Next
答案 1 :(得分:0)
简单,
Dim keysInList1ThatAreNotInList2 = file1.Keys.Except(file2.Keys).ToList
假设list1 is-superset-of
list2。否则只需交换它们。
打印它,
Dim values = From key In keysInList1ThatAreNotInList2 Select file1(key)
Dim str = String.Join(vbCrLf, values)
AddText(str)