Streamreader problems

NoxiousOogle

New member
Joined
Aug 20, 2003
Messages
4
Hi Guys,

Im a noob at VB.Net and also this is my first post here, so I hope its not too stupid :)

Im trying to access a text file, to read the data and put the data into various text boxes. I found the following code in the help section:

Dim sr As StreamReader
Open the file to read.
Sr = File.OpenText("c:\MyFile.txt")
Read each line in the file.
When the end of the file is reached, return the value "-1".
Dim x As String
While sr.Peek <> -1
x = sr.ReadLine()
Console.WriteLine(x)
End While
Tell user the operation is over and close the file.
Console.WriteLine("The end of the stream has been reached.")
sr.Close()

So I figured I would copy and paste this into my code, and then modify it to suit my needs. However, the word "Streamreader" is underlined, meaning an error of some type. When I hover my mouse on the word, it says "Type StreamReader is not defined". Isnt this a keyword, and shouldnt have to be declared like a variable?

Also, the word "File" in the third line is underlined too. Same error message.

Help! Thanks.
 
you could reference the above as PlausiblyDamp said, if you dont you can add IO. infront of the streamreader etc..., also you could shorten your code, heres a quick example :)
Code:
        Dim sr As New IO.StreamReader(New IO.FileStream("C:\myFile.txt", IO.FileMode.OpenOrCreate))

        While Not sr.Peek
            Console.WriteLine(sr.ReadLine)
        End While
        sr.Close()
hope it helps.
 
Originally posted by PlausiblyDamp
At the top of the file add the line
Code:
Imports System.IO

Many thanks :) I had seen that floating around various programs but I didnt see the relationship between that and what I was trying to do.
 
Originally posted by dynamic_sysop
you could reference the above as PlausiblyDamp said, if you dont you can add IO. infront of the streamreader etc..., also you could shorten your code, heres a quick example :)
Code:
        Dim sr As New IO.StreamReader(New IO.FileStream("C:\myFile.txt", IO.FileMode.OpenOrCreate))

        While Not sr.Peek
            Console.WriteLine(sr.ReadLine)
        End While
        sr.Close()
hope it helps.

Ahh, thanks Dynamic, I will try that one later too. :D

Another quick question. When I use console.writeline, nothing displays. I was able to get the imported shown in a text box, which is fine for now, but was wondering what (else) I was doing wrong :)
 
have you got the " output / debug " window showing on your project layout? it should show in there.
you could always try this...
Code:
Debug.WriteLine(sr.ReadLine)
or this...
Code:
Trace.WriteLine(sr.ReadLine)
hope it helps.
and obviously you dont need the IO bit if you put Imports System.IO at the top of your form.
 
Actually, I like it better showing in a text Window, so this will work. I may throw the text into a label too, which I can do :)

Many thanks for the help :) Ive been doing VB6 for a while and VB.NET is a little different, but I like it. :)
 
Back
Top