Text File Read

ADO DOT NET

Centurion
Joined
Dec 20, 2006
Messages
160
I use this code to read and split the first line of a text file:
Visual Basic:
        Dim LineIn As String
        Dim SStr() As String

        FileOpen(1, ("c:\file.txt"), OpenMode.Input)
        LineIn = LineInput(1)
        SStr = LineIn.Split(vbTab)
        FileClose(1)
But in MSDN I red that:
The FileOpen function is provided for backward compatibility and may affect performance. For non-legacy applications, the My.Computer.FileSystem object provides better performance.
-
I also found this example:
Visual Basic:
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\test.txt")
MsgBox(fileReader)
The problem is that I cannot read just the first line of text file using new method.
Also how can I close the file after reading?
Thank you for your help:)
 
If you read the whole length of the file into a string. You can use

Visual Basic:
String.Split(Environment.NewLine())

to get data Line by line.


Additionally, the System.IO namespace will be worth your while read about. The 'My' namespace is Visual Basic 2005 only, so using System.IO will work in any .NET Language.

HTH
 
If you want to split a string into lines using String.Split, you might want to consider including Environment.NewLine, the CarriageReturn, and the LineFeed characters in the separators to cover all your bases (the correct line separator for Win32 is the CrLf but FTP and certain naughty apps might result in only the Cr or only the Lf).
 
Back
Top