Results 1 to 8 of 8

Thread: [Resolved] Reading through a File

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    [Resolved] Reading through a File

    This is my first jump into VB.NET, and I'm trying to make a simple To Do list. I want to open a text file, read each line, and add the data to a ListView. This is the code I have:

    VB Code:
    1. Dim fs As New IO.FileStream("todo.txt", IO.FileMode.Open)
    2. Dim sr As New IO.StreamReader(fs)
    3.  
    4. Do Until sr.Peek = -1
    5.     MsgBox(sr.ReadLine())
    6. Loop
    7.  
    8. fs.Close()

    The file contains three lines, but no message box is ever shown. What am I doing wrong?
    Last edited by The Hobo; Oct 20th, 2003 at 09:51 PM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    299
    Just a note, try this...
    Code:
    Dim sr As New System.IO.StreamReader("todo.txt")
    
    Do Until sr.Peek = -1
        MessageBox.Show(sr.ReadLine())
    Loop
    
    sr.Close()
    That works here...

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Hmm...when I put the todo.txt file in C:\ and then use that path, it works fine.

    Is there an App.Path in VB.NET?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    AppDomain.CurrentDomain.BaseDirectory

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    If the todo.txt is in the same directory as the application, then it should work.

  6. #6
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    Why don't you try:
    VB Code:
    1. Dim sr As New System.IO.StreamReader("todo.txt")
    2. Dim strToDo As String
    3.  
    4. Do Until sr.Peek = -1
    5.     strToDo = sr.ReadLine()  ' Set break point here
    6.     MessageBox.Show(strToDo)
    7. Loop
    8.  
    9. sr.Close()
    ... and see if strToDo actually contains anything. I also use StreamReader for text reading but I do it in a little more complicated way:

    VB Code:
    1. Dim sr As StreamReader
    2.  
    3.         ' Open the file to read.
    4.         Try
    5.             sr = File.OpenText(strDataFileName)
    6.         Catch ex As Exception
    7.             MessageBox.Show(ex.Message, "Error opening file" + strDataFileName + "!")
    8.             Exit Function
    9.         End Try

    In fact from looking at the docs it seems to me that's your problem, you're not actually opening the file.

  7. #7

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by DevGrp
    If the todo.txt is in the same directory as the application, then it should work.
    It is in the directory with the source code. The executable is placed in the Bin directory when compiled. That was my problem.

    Thanks, guys.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    Aha then I guess the Open is implicit and I'm doing an un-needed step.

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