What I am missing to make this piece of Vb code (VB 2005)
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
appear as mostly red, I am running a clean install of MS VS2005 and these codes were recommended to me, am I missing something else that I need to install or declare?
+1
Dalrymple
source
to share
3 answers
FROM Msdn you must do the following to read all lines
Dim Lines As String()
Lines = System.IO.File.ReadAllLines("file.txt")
In 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 application and it works great:
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
+1
source to share
Do you have code surrounded by 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
+1
source to share