Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I need to search a text file with an eight digit integer, to find a whole line which the eight digit integer is contained in.
I tried using streamreader though couldn't get the results I wanted.
Any help would be greatly appreciated.

What I have tried:

option explicit on
imports system.io
Dim search As StreamReader
Dim filename, textline As String, quantity, price, gtin8 As Integer
filename = ("newname.txt")
gtin8 = TextBox1.Text
search = New StreamReader(filename)
If search.contains(gtin8) Then
ListBox1.Items.Add(textstring)
Posted
Updated 17-Jun-16 23:44pm
v2
Comments
phil.o 18-Jun-16 5:25am    
Please use the green "Improve question" button and show the actual code you have tried.

1 solution

In order to search the file for a string, you need to read the file, either completely, or in chunks. Chunks is harder, because you have to be aware that your match string could be "on the border" and spread across two chunks.
But the simplest way is just to read the whole file:
VB.NET
Private Function FileContainsString(path As String, match As String) As Boolean
	Dim s As String = File.ReadAllText(path)
	Return s.Contains(match)
End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900