Results 1 to 5 of 5

Thread: [RESOLVED] problem with reading .txt

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    10

    [RESOLVED] problem with reading .txt

    I am working on a program that i need it to read from a .txt file. My problem is i get an error saying variable not defined for My.Computer.FileSystem.ReadAllText.

    Code:
    Dim fileReader As String
    fileReader = My.Computer.FileSystem.ReadAllText("C:\my vb stuff\stuff.txt")
    MsgBox (fileReader)
    Also i know this will just show all the text is their a way to read one line of this?

    thanks you, blobs
    Last edited by blobs; Dec 4th, 2007 at 10:27 PM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: problem with reading .txt

    Welcome to VBForums

    That code is for VB.Net (specifically 2005 or later I think), so won't work in 'Classic VB' (VB6 or earlier), or earlier versions of VB.Net (2002/2003).

    If you are using VB6 or earlier, you can use the methods shown in the "Files" section of our Classic VB FAQs (in the FAQ forum, which is shown near the top of our home page).

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    10

    Re: problem with reading .txt

    Thanks for the welcome and the help. i forgot that code was for .net lol

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    10

    Re: problem with reading .txt

    This is what i need but, i need it to read like line 1 or like line 1 and 4 how would i do that? Thanks again


    Code:
       Dim sFileText As String
        Dim iFileNo As Integer
       
       
            iFileNo = FreeFile
     
                'open the file for reading
       
            Open "C:\my vb stuff\1\stuff.txt" For Input As #iFileNo
       
          'change this filename to an existing file!  (or run the example below first)
       
           
                'read the file until we reach the end
       
            Do While Not EOF(iFileNo)
      
              Input #iFileNo, sFileText
             
                'show the text (you will probably want to replace this line as appropriate to your program!)
      
           
              MsgBox sFileText
      
            Loop
      
      
                'close the file (if you dont do this, you wont be able to open it again!)
     
            Close #iFileNo

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: problem with reading .txt

    The "Input" line is what actually reads the values from the file, so you could use something like a For loop to 'ignore' the rows you don't want - or you could use an alternative method like loading the entire file at once (examples in the FAQs) and separate it into an array of lines using the Split function, eg:
    Code:
    Dim strLines() as String
      strLines = Split(strFileText, vbNewLine)
    
      MsgBox "First line: " & strLines(0)
      MsgBox "Fourth line: " & strLines(3)

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