|
-
Sep 8th, 2011, 06:18 AM
#1
Thread Starter
Hyperactive Member
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
-
Sep 8th, 2011, 07:07 AM
#2
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
-
Sep 8th, 2011, 12:04 PM
#3
Thread Starter
Hyperactive Member
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
-
Sep 8th, 2011, 12:08 PM
#4
Re: adding .txt files to my program
Then just do not copy it to the out directory and reference it via my.resources.***
-
Sep 8th, 2011, 12:12 PM
#5
Thread Starter
Hyperactive Member
Re: adding .txt files to my program
 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
-
Sep 8th, 2011, 12:18 PM
#6
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.
-
Sep 8th, 2011, 12:23 PM
#7
Thread Starter
Hyperactive Member
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
-
Sep 8th, 2011, 12:37 PM
#8
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))
-
Sep 8th, 2011, 12:46 PM
#9
Thread Starter
Hyperactive Member
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
-
Sep 8th, 2011, 12:52 PM
#10
Thread Starter
Hyperactive Member
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
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
|