adding .txt files to my program
Hi, i have a program that when you click a button it randomly gets a random line from the file and adds it to a textbox.
Well when i give ppl the program it needs to have the .txt file in the same dir right.
But how would i add the file to the program itself ?
Or
Add the contents of the file to the project ? maybe a class or something idk.
Thanks for any help
Re: adding .txt files to my program
Right-click the project, select "Add" -> "Existing Item" ... find your txt file click OK. Then click on the file in the solution explorer, right-click, select Properties... make sure the Build Action is "None" and the Copy option is set to Copy to Output folder. Now it'll go with the application...
When it you give it to others, simply copy the contents of the bin/debug or bin/release folder.
-tg
Re: adding .txt files to my program
well the file is still there as outside of the program, reason i asked for as it would be easyer if they did not see the .txt file
Thanks
Re: adding .txt files to my program
Then just do not copy it to the out directory and reference it via my.resources.***
Re: adding .txt files to my program
Quote:
Originally Posted by
ident
Then just do not copy it to the out directory and reference it via my.resources.***
Ok so if i do this how would i read a random line from it with this:
Code:
http://www.vbforums.com/showthread.php?p=4063600#post4063600
could be 2 birds killed with 1 stone :)
Thanks
Re: adding .txt files to my program
Just create an array reading each line. Create a random number within the bounds of the array count. Then just read the returned random line number.
Re: adding .txt files to my program
Hmm, im new to reading from files ect, I have no idea how to do this and from the other thread like you said too many bad habits. I cant learn if i dont know where to start, i normally learn from examples but some codes are bad habits.
Thanks for any help
Re: adding .txt files to my program
So what class handles reading of files? Tkae the time to read System.IO.File namespace http://msdn.microsoft.com/en-us/libr...m.io.file.aspx
To create an array of each line
Code:
Dim lines() As String = Strings.Split(My.Resources.MyText, Strings.Chr(13) & Strings.Chr(10))
Re: adding .txt files to my program
You see there is nothing atm, just 1 textbox and 1 button for me to learn this.
I have tryed this but get errors
Code:
Dim RandomNumber As New Random()
Dim Tr As IO.TextReader = System.IO.File.OpenText(My.Resources.fname)
Dim FileLines() As String = Split(Tr.ReadToEnd(), vbCrLf)
Tr.Close()
Dim MyRandomLine As String = FileLines(RandomNumber.Next(0, UBound(FileLines)))
TextBox1.Text = MyRandomLine
error catches on this line "System.IO.File.OpenText(My.Resources.fname)" and says "Illegal characters in path."
Thanks
Re: adding .txt files to my program
nvm, i got it with this:
VB CODE:
Code:
Dim RandomNumber As New Random()
Dim lines() As String = Strings.Split(My.Resources.fname, Strings.Chr(13) & Strings.Chr(10))
Dim MyRandomLine As String = Lines(RandomNumber.Next(0, UBound(Lines)))
TextBox1.Text = MyRandomLine
How does this look ? it works fine tho but i may have overlooked something
Thanks