im new to .net and my book doesn't cover this simple request for reading in files

  • Thread starter Thread starter locutus
  • Start date Start date
L

locutus

Guest
Hi
I have just started using vb and the .net enviroment and I am going through my teach yourself book . I decided to have a break and create a little application to practise some of the techniques I had learned.

I am trying to open a text file and loop through and read each line of the file into a holder of some type ( will prob use an array for the sake of using them in my example )

heres what I have so far
dim fsout as system.io.filestream
dim filename as string
dim oreader as system.io.streamreader
dim scontents as string
dim bexists as boolean

filename = "c:\login.txt"
bexists = system.io.file.exists(filename)

try
open the file
fsout = new system.io.filestream(filename,system.io.filemode.openorcreate,system.io.fileaccess.read)

oreader = new system.io.streamreader(fsout)
think I need a while in here to loop
scontents = oreader.readline

this is what I have so far , all I want to do is loop through to the end of the file and be able to assign each loop through to a holder of some type , but this new approach in .net is very confusing and I am struggling to get my head around it.
Would anyone be able to help with this small learning example of mine . If you supply the code to help could you try and explain what is going on also with the code

Many thx for your time
locutus
 
To make raeding the stream easier, you should declare a new
StringReader object to read from the file stream. In the declaration
of your form, add this:

Code:
Dim sLines() As String

Then, after opening the file, read the entire thing, and then use
Split to divide it up into lines:

Code:
scontents = oreader.ReadToEnd

sLines = Split(scontents, vbCrLf) 
 You could also use sLines = scontents.Split(vbCrLf) if desired

Now sLines is an array of strings that holds each lines text, and
you can now loop through them as you like.

HTH
 
Back
Top