Results 1 to 10 of 10

Thread: Load Word List From Resource Text File

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    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:
    1. Dim reader1 As New System.IO.StreamReader("c:\1.txt")
    2.         For i As Integer = 0 To 25
    3.             BlanksOne(i) = reader1.ReadLine()
    4.         Next i

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    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.

  4. #4
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Load Word List From Resource Text File

    Just use:

    vb.net Code:
    1. Dim txtRead As New System.IO.StreamReader(My.Resources.MyTextFile)
    2. 'Do stuff.
    3. txtRead.Close()

    I think that will work.

    And if you want them in a list that varies in size:
    Class-level Code:
    1. Dim lines As List(Of String) = New List(Of String)

    After previous code Code:
    1. While txtRead.Peek() > -1
    2.      lines.Add(txtRead.ReadLine())
    3. End While
    Last edited by minitech; May 13th, 2009 at 08:09 PM. Reason: Added list code

  5. #5
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Re: Load Word List From Resource Text File

    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  6. #6
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    493

    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.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    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?

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    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
  •  



Click Here to Expand Forum to Full Width