如何从文本文件vb.net中删除重复的行

时间:2016-02-10 05:09:23

标签: vb.net

我想从包含for的文本文件中删除重复的行 例如:

.abc
.def
.ghi
.abc
.abc

获得结果

.abc
.def
.ghi

1 个答案:

答案 0 :(得分:2)

使用HashSet(Of T)的{​​{1}}方法添加以仅设置新项目。 HashSet(Of T) Class

Dim path As String = "yourPath"
Dim lines As New HashSet(Of String)()
'Read to file
Using sr As StreamReader = New StreamReader(path)
    Do While sr.Peek() >= 0
        lines.Add(sr.ReadLine())
    Loop
End Using

'Write to file
Using sw As StreamWriter = New StreamWriter(path)
    For Each line As String in lines
        sw.WriteLine(line)
    Next
End Using