tags:

views:

232

answers:

4
For Each line As String In System.IO.File.ReadAllLines("file.txt")

' Do Something' Next

and

Using f As System.IO.FileStream = System.IO.File.OpenRead("somefile.txt")
    Using s As System.IO.StreamReader = New System.IO.StreamReader(f)
            While Not s.EndOfStream
                    Dim line As String = s.ReadLine

                    'put you line processing code here

            End While
    End Using

End Using

are both showing as mostly red, I'm running a clean install of MS VS2005 and these codes were both recomended to me, am I missing something else I need to install or declare?

+1  A: 

FROM Msdn you should do the following to read all lines

Dim Lines As String()
Lines = System.IO.File.ReadAllLines("file.txt")

For the second example something like this might work

Dim sr as New StreamReader("somefile.txt")
Dim line as String = sr.ReadLine()
Do While Not line is Nothing
    line = sr.ReadLine()
    'do something else
Loop

I just created the following VB.Net Console app and it works fine:

Imports System.IO

Module Module1

Sub Main()
    Dim sr As New StreamReader("somefile.txt")
    Dim line As String = sr.ReadLine()
    Do While Not line Is Nothing
        line = sr.ReadLine()
        'do something else
    Loop

End Sub

End Module
Mauro
Still getting syntax errors with those :(
Yeah even creating a new project with just that and nothing else is getting these two lines as red:Dim sr As New StreamReader("somefile.txt") Dim line As String = sr.ReadLine()
what kind of project are you creating?
Mauro
+1  A: 

Do you have your code surrounded by a class and method?

Public class CodeClass
    Public Sub CodeMethod

        Using f As System.IO.FileStream = System.IO.File.OpenRead("somefile.txt")
            Using s As System.IO.StreamReader = New System.IO.StreamReader(f)
                While Not s.EndOfStream
                    Dim line As String = s.ReadLine

                    //Non-vb comment for easier to read SO code
                End While
            End Using
        End Using

    End Sub
End Class
Gavin Miller
Even just straight copying and pasting that shows error for the first line saying expected end of statement. :(
That's a hard one... anymore information you could provide to go on; such as a screen shot?
Gavin Miller
I think you are missing the line breaks Dalrymple, I copied and pasted direct from SO and found that it had stripped the line breaks. Once I added them it worked fine.
Mauro
A: 

Are you creating the project as a VB lanugage project or something else?

Gavin Miller
Start--> Programs--> Microsoft Visual Basic 6.0 ==> new standard exe view code copy paste.is essentially what I did here :/ I must be doing something wrong but all my other little apps work fine, just rying to do some simple I/O and can't do it ???
vb 6.0 ? thats the problem!I thought you said it was VB 2005? that should be listed as Microsoft Visual Studio 2005..not VB 2006
Mauro
ack I thought they were the same thing.
My VB6 is rusty, but drop the public class stuff from the previous code post.
Gavin Miller
No love for effort... :*(
Gavin Miller
A: 

for vb6.0 you need

Dim value As String = My.Computer.FileSystem.ReadAllText(file)
Mauro