Reputation:
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?
Upvotes: 1
Views: 472
Reputation: 4511
for vb6.0 you need
Dim value As String = My.Computer.FileSystem.ReadAllText(file)
Upvotes: 0
Reputation: 4511
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
Upvotes: 1
Reputation: 43815
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
Upvotes: 1