Best way to include a text file in final project

mattyjim

New member
Joined
Oct 26, 2009
Messages
2
Programming Experience
1-3
Hey!


No doubt this is a total noob :o question, but here goes:

I'm writing a very basic console application (vb.net 3.5) (just for learning purposes) that reads lines from a text file (in the Solution Items folder) and then displays them in console window.

So far, so good; everything works fine.


The only problem is that at the moment, unless I use a complete path to my .txt file (e.g. "C:\Users\Me\Documents\Visual Studio 2008\Projects\MyTest\TestInputFile.txt"), I get a FileNotFoundException.


Obviously, if the application was published and made available to users via an installer or whatever, the input file would no longer be in this location!

Can anybody tell me the best way to reference the file so that my application will be able to read from it at all times?
 
Instead of
VB.NET:
Expand Collapse Copy
Dim file1 As String = "C:\My Documents\file.txt"
Use
VB.NET:
Expand Collapse Copy
Dim file1 As String = My.Application.Info.DirectoryPath.ToString & ".\file.txt"
This will get the file that is located in the same directory as the program :)
 
Thanks, Untamed!

That seems to do the trick. Including the file as a resource seems to be an option as well, depending on the objective of the application.


Thanks a lot for your help!:)
 
Back
Top