Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I use this code to read and split the first line of a text file:

       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:

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:)

Posted

If you read the whole length of the file into a string. You can use

 

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

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

  • Leaders
Posted
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).
[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...