6 Replies - 4814 Views - Last Post: 14 June 2009 - 11:29 AM Rate Topic: -----

#1 zql  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 14-June 09

Storing a text file from my.resources into a collection

Post icon  Posted 14 June 2009 - 01:10 AM

Hello, I've spent about two hours trying to find the solution to this but to no avail. MSDN only discusses strings, images, audio, etc.. but does not cover text files.

I have a text file with about 6k lines of short strings, each on its own line, added to my project's resources and I'm trying to .add each line to a collection. I've tried about 7 or 8 things, not all using StreamReader, but I can't find documentation on this anywhere.

Here is the last thing I tried:

Dim JunkStrings As New Collection				
Dim sr As StreamReader = New StreamReader(My.Resources.shorts)

Do While sr.Peek() >= 0
		 JunkStrings.Add(sr.ReadLine())
Loop
sr.Close()



As just a .txt file on my hard drive, this is what I was doing:

Dim TheFileName As String
Dim JunkStrings As New Collection
Dim LineofText As String
TheFileName = "shorts.txt"

FileOpen(1, TheFileName, OpenMode.Input)
Do Until EOF(1)
	LineofText = LineInput(1)
	JunkStrings.Add(LineofText)
Loop
FileClose(1)




I just can't find the equivalent to this that reads lines from a text file out of my.resources. Any help would be greatly appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Storing a text file from my.resources into a collection

#2 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: Storing a text file from my.resources into a collection

Posted 14 June 2009 - 02:40 AM

Delete content, on wrong track.

This post has been edited by June7: 14 June 2009 - 02:43 AM

Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: Storing a text file from my.resources into a collection

Posted 14 June 2009 - 03:44 AM

You can a Resource based TextFile as if it was a string. To separate it line split the string and the newline character(s).
Was This Post Helpful? 0
  • +
  • -

#4 zql  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 14-June 09

Re: Storing a text file from my.resources into a collection

Posted 14 June 2009 - 10:55 AM

I've tried what you suggested, this is my code:
Dim ln, curline, letter As String
Dim linestart, i, charsInFile As Integer

linestart = 1
charsInFile = My.Resources.shorts.Length
For i = 0 To charsInFile - 1
	letter = My.Resources.shorts.Substring(i, 1)
	If letter = Chr(13) Then						
		JunkStrings.Add(My.Resources.shorts.Substring(linestart, i))
		linestart = i + 1
		i += 1  'skip CR char
	End If
Next i

But this is resulting in an ArgumentOutOfRange exception. Is there a proper way to separate strings by CR's? Moving this text file into the project resources was supposed to make it easier for deployment, but I've spent a total of 5 hours just trying to figure out how to read it now! I feel like a beginner all over again lol

View PostAdamSpeight2008, on 14 Jun, 2009 - 02:44 AM, said:

You can a Resource based TextFile as if it was a string. To separate it line split the string and the newline character(s).

Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: Storing a text file from my.resources into a collection

Posted 14 June 2009 - 11:12 AM

Use split it a lot simpler.
Dim lines() as string=My.Resources.TextFile1.Split(Enviroment.NewLine)
For Each line As String In lines
 ListBox.Iitems.Add(line)
Next


Was This Post Helpful? 0
  • +
  • -

#6 zql  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 14-June 09

Re: Storing a text file from my.resources into a collection

Posted 14 June 2009 - 11:15 AM

Okay, I solved it. I've been working with VB6 up until this year so I still had a bit to learn about StringReader. I did try this already, but incorrectly I suppose.
Anyway, here is the working code to answer my own problem:
Dim JunkStrings as New Collection
Dim sr As New StringReader(My.Resources.shorts)
While sr.Peek <> -1
	Dim s As String = sr.ReadLine
	JunkStrings.Add(s)
End While

Thank you everyone, for making me feel like I was not alone while working through this =)

Thank you for this method also! You and I were posting at the exact same moment or I would have tried this, but I will remember .split for the future as well.

View PostAdamSpeight2008, on 14 Jun, 2009 - 10:12 AM, said:

Use split it a lot simpler.
Dim lines() as string=My.Resources.TextFile1.Split(Enviroment.NewLine)
For Each line As String In lines
 ListBox.Iitems.Add(line)
Next


Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: Storing a text file from my.resources into a collection

Posted 14 June 2009 - 11:29 AM

I didn't know about StringReader, so I learned something new. Placing under the IO namespace is bit strange, to me it's Text.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1