|
-
Oct 20th, 2005, 08:57 AM
#1
Thread Starter
New Member
-
Oct 20th, 2005, 09:09 AM
#2
Fanatic Member
Re: reading a text file
Try using the StreamReader. That object allows you to ReadLine().
What object are you using to open the file and read it?
-
Oct 20th, 2005, 03:11 PM
#3
Re: reading a text file
As Aspnot says, use a StreamReader to read the data from the file. You would then use the Peek method to determine whether there is still data to be read, which checks for data but does not actually read it.
-
Oct 21st, 2005, 07:41 PM
#4
Thread Starter
New Member
Re: reading a text file
I am using a click event on a button
In a world of terror lives a world of fear!
-
Oct 21st, 2005, 08:25 PM
#5
Re: reading a text file
 Originally Posted by Aphid2005
I am using a click event on a button
That makes no difference. You can put whatever code you want wherever you want. You can use a StreamReader in the Button's Click event handler or you can put it in a method that you call from the event handler.
-
Oct 21st, 2005, 10:53 PM
#6
New Member
Re: reading a text file
Dim sr As IO.StreamReader =IO.File.OpenText("nameoffile.txt)
Do while sr.Peek <> - 1
username = sr.ReadLine()
password = sr.ReadLine()
Loop
The only problem with StreamReader is that you cannot choose which line you want to read when using the ReadLine method. It has always got to go line by line.
-
Oct 22nd, 2005, 12:33 AM
#7
Re: reading a text file
Make an If Condition inside your loop to evaluate what the line is, and execute the code you want...
VB Code:
Do while sr.Peek <> - 1
Dim strLine as String
strLine = sr.ReadLine()
If strLine = "" then
'do nothing
Else
'add more code or If statements to evaluate the string further....
username = strLine 'just an example
End If
Loop
-
Oct 22nd, 2005, 01:24 AM
#8
Re: reading a text file
or simplify gigemboy's code:
VB Code:
Do while sr.Peek <> - 1
Dim strLine as String = sr.ReadLine()
If strLine <> "" then
username = strLine 'just an example
End If
Loop
-
Oct 22nd, 2005, 04:06 PM
#9
Re: reading a text file
Juggalo, won't that create and dispose the variable strline every time?
HEre's how I do it..
VB Code:
Dim sr As New streamreader(filename)
Dim line As String
Do
line = sr.readline
'Check, store, manipulate etc variable "line"
Loop Until line Is Nothing
sr.Close()
line.Dispose()
Bill
-
Oct 22nd, 2005, 05:06 PM
#10
Re: reading a text file
It won't dispose it, the code works, and you really don't have to call your line.dispose in your code, conipto, since you declared it with the "Dim" statement. The line is automatically disposed once it is out of scope...
Last edited by gigemboy; Oct 22nd, 2005 at 08:28 PM.
-
Oct 22nd, 2005, 07:20 PM
#11
Re: reading a text file
You can declare the string variable outside the loop to avoid a new variable being created each iteration, but note that the String class does not have a Dispose method, so you just leave it up to the GC to clean up the managed resources when its ready.
-
Oct 22nd, 2005, 08:25 PM
#12
Thread Starter
New Member
In a world of terror lives a world of fear!
-
Oct 22nd, 2005, 08:52 PM
#13
Re: reading a text file
Don't forget to resolve your thread from the Thread Tools menu.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|