Reputation: 33
Found a question already answering this? Please link it. I've not found anything.
How do I read a .txt
file which I've placed into my Resources
folder? The following code doesn't seem to find the file, even I know I've put it there. Maybe I'm doing something wrong?
Using textreader As System.IO.TextReader = File.OpenText("Resources/map.txt")
This doesn't seem to work. Neither does:
Using textreader As System.IO.TextReader = File.OpenText(My.Resources.Map)
(Map
is the name I've given it inside Visual Studio).
Alternatively, how would I find the program's Debug/Resources
folder within the file system? What method do I use?
Any ideas what I can do to solve this? And when someone is using my program and wants to save the map file, where would be best programming practice to save it? (Since I can't put it in Resources
because that's within the .exe
). Just C:\Programs\ A new folder just for my program
?
N.B. VB.NET WinForms
Upvotes: 0
Views: 12881
Reputation: 71
I'm using Visual Studio 2015 and I needed to find a way to read text files within my program. This is what I found: To add an existing text file as a resource Right Click on [project Name - your project] in solution explorer, then Select Properties, then Resources, Then add the text file as an existing text file (e.g. - you create myfile.txt and put it where you can find it) You can then read the text file as a stream of lines or as a single string
To read a resource text file as a stream of lines. This example will display the first line. You can then use streamreader code to access all the lines.
Dim lineA As String = String.Empty
Using reader As TextReader = New StringReader(My.Resources.myfile)
lineA = reader.ReadLine
MsgBox(lineA)
End Using
To read the resource text file as a single string and display (e.g.) the first 5 characters
Dim LineB as String = MyResources.myfile
MsgBox(Mid(LineB,1,5))
Upvotes: 1
Reputation: 2152
I believe this question was best answer previously here: StackOverflow Question/Answer
Dim content As String = My.Resources.Map
Upvotes: 1