Results 1 to 3 of 3

Thread: [RESOLVED] Reading from a file

  1. #1

    Thread Starter
    Lively Member drdress's Avatar
    Join Date
    Jul 2009
    Location
    Copenhagen, Denmark
    Posts
    76

    Resolved [RESOLVED] Reading from a file

    I'm trying to read lines from a file using:

    Code:
    Dim FileLine As String = ""
    Dim File As Integer = FreeFile()
    
    Open "C:\text.txt" for Line Input # File
    But I get this error:

    'Open' is not declared. File I/O functionality is available in the 'Microsoft.VisualBasic' namespace.

    I tried to add Microsoft.VisualBasic as a reference, but it didn't work. Any help?

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Reading from a file

    What you're trying todo is to use old VB6 code. That will not work. Add an Import statement to include System.IO and use code similar to this:
    Code:
    Dim reader As New StreamReader("c:\text.txt")
    Dim firstLine As String = reader.ReadLine()
    Dim theRestOfTheText As String = reader.ReadToEnd()
    reader.Close
    OK, you probably don't just want to read the first line and then the rest but I just wanted to show what the two most used methods look like. If you want to read it line by line you can do something like this:
    Code:
    Do While Not reader.EndOfStream
        fileLine = reader.ReadLine() '<- This assumes you have already declared fileLine as a string
        'do whatever
    Loop 
    reader.Close

  3. #3

    Thread Starter
    Lively Member drdress's Avatar
    Join Date
    Jul 2009
    Location
    Copenhagen, Denmark
    Posts
    76

    Re: Reading from a file

    That did the trick. Thank!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width