|
-
Oct 20th, 2003, 07:32 PM
#1
Thread Starter
Stuck in the 80s
[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:
Dim fs As New IO.FileStream("todo.txt", IO.FileMode.Open)
Dim sr As New IO.StreamReader(fs)
Do Until sr.Peek = -1
MsgBox(sr.ReadLine())
Loop
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.
-
Oct 20th, 2003, 08:09 PM
#2
Hyperactive Member
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...
-
Oct 20th, 2003, 08:18 PM
#3
Thread Starter
Stuck in the 80s
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?
-
Oct 20th, 2003, 09:27 PM
#4
AppDomain.CurrentDomain.BaseDirectory
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Oct 20th, 2003, 09:28 PM
#5
Frenzied Member
If the todo.txt is in the same directory as the application, then it should work.
-
Oct 20th, 2003, 09:45 PM
#6
Lively Member
Why don't you try:
VB Code:
Dim sr As New System.IO.StreamReader("todo.txt")
Dim strToDo As String
Do Until sr.Peek = -1
strToDo = sr.ReadLine() ' Set break point here
MessageBox.Show(strToDo)
Loop
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:
Dim sr As StreamReader
' Open the file to read.
Try
sr = File.OpenText(strDataFileName)
Catch ex As Exception
MessageBox.Show(ex.Message, "Error opening file" + strDataFileName + "!")
Exit Function
End Try
In fact from looking at the docs it seems to me that's your problem, you're not actually opening the file.
-
Oct 20th, 2003, 09:50 PM
#7
Thread Starter
Stuck in the 80s
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.
-
Oct 21st, 2003, 05:47 AM
#8
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|