Karim Mamdouh
Karim Mamdouh

Reputation: 55

how to remove duplicate line from text file vb.net

I want to remove duplicated line from text file that contain for example :

.abc
.def
.ghi
.abc
.abc

to get the result

.abc
.def
.ghi

Upvotes: 0

Views: 2880

Answers (1)

Fabio
Fabio

Reputation: 32445

Use .Add() method of HashSet(Of T) which adding to set only new items. 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

Upvotes: 2

Related Questions