I am writing a program that manipulates text file using console application. What I want to do is to delete 1 specific line based on the ID entered by the user.
For example:
DR-01|Coke|25.80
DR-02|Sprite|25.80
DR-03|Sarsi|25.80
DR-04|Coke|25.80
When the user entered DR-02, the result would be like this:
DR-01|Coke|25.80
DR-03|Sarsi|25.80
DR-04|Coke|25.80
My program should delete the line with the ID DR-02, but my program deletes items from DR-02 to DR-04 instead of DR-02 only.
My code:
Module Mod1
Dim txtDrink As String = Application.StartupPath & ("\drink.txt")
sub main
CountDrinkIdToDelete
end sub
Sub CountDrinkIdToDelete()
Dim lines As New List(Of String)
Dim inp As String
Dim intNum As Integer
ViewDrinkRecord()
Console.Write("Enter Dessert ID: ")
inp = Console.ReadLine.ToUpper.Trim
If inp.Length = "5" Then
Call DrinkTxtFile()
Using reader As StreamReader = New StreamReader(txtDrink)
Do While (True)
Dim line As String = reader.ReadLine
lines.Add(line)
If line Is Nothing Then
Exit Do
End If
intNum += 1
If Left(line, 5).Contains(inp) Then
lines.RemoveAt(intNum - 1) ' this line removes the item
Exit Do
End If
Loop
End Using
End If
Using sw As New StreamWriter(txtDrink)
For Each line As String In lines
sw.WriteLine(line)
Next
End Using
Console.WriteLine("Successful")
End Sub
Sub DrinkTxtFile()
If File.Exists(txtDrink) = False Then
File.Create(txtDrink).Dispose()
End If
End Sub
End Module
Feel free to share your ideas. Thanks in advance,, :)