Reading Text From a File

adey

New member
Joined
Mar 12, 2007
Messages
1
Programming Experience
Beginner
Hi, i am trying to read text from a text file. The text i need is after the word "ISPSignup" in the text file. However the line that "ISPSignup" is located on is random on different OS's, so i cannot simply grab the text from a specific line in the file. Then the text needs to be displayed in Lbl_text label. Is there any way of searching the file for ISPSignup and displaying the text that is included on that line?
 
Use the StreamReader class and its ReadLine method, it will return a string for each line that you can investigate with IndexOf or Contains methods.
 
Use The Stream Reader

Dim streamtodisplay As StreamReader
streamtodisplay = New StreamReader(" directory and file name goes here ")
textbox.Text = streamtodisplay.ReadToEnd
streamtodisplay.Close()

textbox.text is a textbox control so you know the stream reader has worked

hope this helps m8
 
Working way too hard folks!

VB.NET:
My.Computer.FileSystem.ReadAllText(fileName)

Once you get the text, you can use the IndexOf method to locate your text and go from there.

For example:

VB.NET:
Dim myText As String = My.Computer.FileSystem.ReadAllText(fileName)
Dim textLocation as Integer = myText.IndexOf("ISPSignup")
'Go from here to do what you need to do
 
Working way too hard folks!

VB.NET:
My.Computer.FileSystem.ReadAllText(fileName)

New System.IO.StreamReader(fileName).ReadToEnd()

can be used to achieve the same effect in all .NET languages, not just VB

although, i admit, it needs more keypresses than yours! ;)


VB.NET:
Dim myText As String = My.Computer.FileSystem.ReadAllText(fileName)
[SIZE=2][COLOR=#0000ff][COLOR=blue]Dim myText As String = [/COLOR][/COLOR][/SIZE][COLOR=blue][SIZE=2]New[/SIZE][SIZE=2] System.IO.StreamReader(fileName[/SIZE][SIZE=2]).ReadToEnd()[/SIZE][/COLOR]


Of course, if the text file is 2 gigs, and you want from offset 100 - 400, then you might want to rethink the sensibility of reading in 2 gigs just to pull 300 bytes from the first percent of it.. :D
 
New System.IO.StreamReader(fileName).ReadToEnd()
Wouldn't that lock the file until the streamreader is disposed by GC (maybe hours later) ? With such approach not catching the object reference you certainly miss the option of closing/disposing the stream, which you should.
Note: Always call Dispose before you release your last reference to the TextReader. Otherwise, the resources it is using will not be freed until the garbage collector calls the TextReader object's Finalize method.
StreamReader.Close does also call Dispose.
 
Back
Top