Reading txt files

vblearner

New member
Joined
Feb 8, 2008
Messages
1
Programming Experience
Beginner
Someone plaese help me. I've tried every method in books and tutorials to read txt files into a textbox in vb.net and I KEEP getting an error saying "Could not find file "xxx.txt". This seems like a very simple peice of code. I've put the file in different directories and tried putting it in the "bin" folder where the application is located. No luck. I've never been able to get vb to find a file anywhere..Can anyone suggest any reason why the following codes dont work for me in my Click event?

Dim Rt As New System.IO.StreamReader("C:\test.txt")
Textbox1.Text = Rt.ReadToEnd
Rt.Close()

Then I tried this with the file in the \bin folder.
(Imported System.IO)

Dim TestFile As StreamReader
Dim Line As StreamWriter

TestFile = File.Open(Directory.GetCurrentDirectory() & "\test.txt")
Line = TestFile.ReadLine()
 
If it says the file can't be found then the file doesn't exist. There's no other reason for it.

Also, this:
VB.NET:
Directory.GetCurrentDirectory()
is not going to return the path to your bin folder if you're using VB2005. Frankly, there's absolutely no reason to ever put anything in the bin folder with VB2005.

How have you created this text file? Have you added it to the project in the Solution Explorer?
 
Back
Top