Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi Guys,

 

I'm a noob at VB.Net and also this is my first post here, so I hope it's not too stupid :)

 

I'm 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". Isn't this a keyword, and shouldn't have to be declared like a variable?

 

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

 

Help! Thanks.

  • Leaders
Posted

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, here's a quick example :)

       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.

Posted
At the top of the file add the line

Imports System.IO

 

Many thanks :) I had seen that floating around various programs but I didn't see the relationship between that and what I was trying to do.

Posted
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, here's a quick example :)

       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 :)

  • Leaders
Posted

have you got the " output / debug " window showing on your project layout? it should show in there.

you could always try this...

Debug.WriteLine(sr.ReadLine)

or this...

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.

Posted

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 :) I've been doing VB6 for a while and VB.NET is a little different, but I like it. :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...