|
-
May 13th, 2009, 07:08 PM
#1
Thread Starter
Hyperactive Member
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
-
May 13th, 2009, 07:39 PM
#2
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?
-
May 13th, 2009, 07:58 PM
#3
Thread Starter
Hyperactive Member
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.
-
May 13th, 2009, 08:02 PM
#4
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
Last edited by minitech; May 13th, 2009 at 08:09 PM.
Reason: Added list code
-
May 13th, 2009, 09:11 PM
#5
Fanatic Member
Re: Load Word List From Resource Text File
-
May 13th, 2009, 09:46 PM
#6
Hyperactive Member
Re: Load Word List From Resource Text File
Try This:
Code:
Textbox.Text = My.Resources.MyTextFile
Last edited by VB6Learner; May 13th, 2009 at 09:57 PM.
-
May 13th, 2009, 09:51 PM
#7
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.
-
May 13th, 2009, 10:27 PM
#8
Thread Starter
Hyperactive Member
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?
-
May 13th, 2009, 10:35 PM
#9
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.
-
May 15th, 2009, 01:08 PM
#10
Re: Load Word List From Resource Text File
Sorry, I didn't notice until I tried that, only some are opened as a stream...
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
|