Load Word List From Resource Text File
I can't figure out how to access the text file that I added to the project as a resource file. I want a list of words to be part of the .exe file, not contained in a separate .txt file on the hard drive.
What do I need to change?
vbcode Code:
Dim reader1 As New System.IO.StreamReader("c:\1.txt")
For i As Integer = 0 To 25
BlanksOne(i) = reader1.ReadLine()
Next i
Re: Load Word List From Resource Text File
1) Have you looked at the My.Resources.ResourceManager?
2) If it's the same 26 words then why not create a class level array that is the text file's contents?
Re: Load Word List From Resource Text File
1. No, but I will.
2. It's not just 26 words. That's a small file but I have others with 170k words.
Re: Load Word List From Resource Text File
Just use:
vb.net Code:
Dim txtRead As New System.IO.StreamReader(My.Resources.MyTextFile)
'Do stuff.
txtRead.Close()
I think that will work.
And if you want them in a list that varies in size:
Class-level Code:
Dim lines As List(Of String) = New List(Of String)
After previous code Code:
While txtRead.Peek() > -1
lines.Add(txtRead.ReadLine())
End While
Re: Load Word List From Resource Text File
Re: Load Word List From Resource Text File
Try This:
Code:
Textbox.Text = My.Resources.MyTextFile
Re: Load Word List From Resource Text File
You don't use a StreamReader at all because there is no stream to read. There is no file. When you add a resource the contents of the file is embedded into your EXE. As VB6Learner has demonstrated, My.Resources returns the contents of the used-to-be-file as a single String. If you want to read that String like you would a file then you can use a StringReader, but you may as well just use String.Split to break it into lines.
Re: Load Word List From Resource Text File
Actually, I've only added the txt file to the project. (been working on some other stuff) is there more to "adding a resource" than just adding a text file to the project?
Re: Load Word List From Resource Text File
A file is just a file. Resources are compiled into your executable. Open the Resources property page and add your file there, then you can access its content via My.Resources.
Re: Load Word List From Resource Text File
Sorry, I didn't notice until I tried that, only some are opened as a stream...