Ok, so I have to save my information to an external text file. I have user input boxes and need to save the inputs and the return to a simple text file. What I would like to do, if it is possible, is when the user presses the "Calculate" button is have the information store in the text file. Is this possible and how could I go about setting something like this up? This is unfamiliar territory for me and I'm not sure how to even begin. Could someone post a sample for me on say storing two text boxes to a file upon a button click?
Also, is an Excel file considered a simple text file or should it be an actual .txt file ?
Simple text file
Page 1 of 18 Replies - 2886 Views - Last Post: 16 March 2013 - 03:09 PM
Replies To: Simple text file
#2
Re: Simple text file
Posted 16 March 2013 - 08:14 AM
Gave it a whirl. I added this code into the btnCalculate method, when I ran the program I got an error that access to the storage path is denied.
Dim storage As StreamWriter storage = File.AppendText("C:\Storage.txt") storage.WriteLine(ComboBox1.Text) storage.WriteLine(TextBox1.Text) storage.WriteLine(TextBox2.Text) storage.WriteLine(TextBox3.Text) storage.WriteLine(TextBox4.Text) storage.Close()
This post has been edited by Zilna: 16 March 2013 - 08:17 AM
#3
Re: Simple text file
Posted 16 March 2013 - 08:46 AM
It could be correct, you may not have the privileges to write to the directory C:\.
For example in Vista and later OSes that directory is protected.
It also indicates (to me you don't know the correct places you write file to, and also how to correctly refer to them. Hard coding file paths into your program makes it really brittle, as the path (more than likely) will be different on another computer.
Use My Documents instead, or create folder in \AppData (that's is purpose) and use that.
For example in Vista and later OSes that directory is protected.
It also indicates (to me you don't know the correct places you write file to, and also how to correctly refer to them. Hard coding file paths into your program makes it really brittle, as the path (more than likely) will be different on another computer.
Use My Documents instead, or create folder in \AppData (that's is purpose) and use that.
#4
Re: Simple text file
Posted 16 March 2013 - 09:01 AM
Are you writing vb.net or vb6.
I think it's vb.net. Do you?
I think it's vb.net. Do you?
This post has been edited by AdamSpeight2008: 16 March 2013 - 09:01 AM
#5
Re: Simple text file
Posted 16 March 2013 - 09:21 AM
VB6, and yes I am unclear how to write to a file. How would I put the directory to save the file to the desktop? Other than that, does the rest of the code look right? I got it from my text book which only used fileName.txt for the file but I wouldn't even know the location that the file is being saved to that way.
#6
Re: Simple text file
Posted 16 March 2013 - 10:55 AM
I don't which text book you a referring to as I'm not in your class (am I).
So I don't if the code looks right.
I know it doesn't look right for vb6 though, because of the follow things
What IDE are you using? What it say in the Help -> About window?
So I don't if the code looks right.
I know it doesn't look right for vb6 though, because of the follow things
- StreamWriter is .net type.
- using File.Append
What IDE are you using? What it say in the Help -> About window?
#7
Re: Simple text file
Posted 16 March 2013 - 02:02 PM
I apologize, the code came from the text and I know that you do not have the book. You are right actually, sorry. I am using Microsoft Visual Studio 2010 and it is VB.Net not VB. I am new to this and trying to learn by our book but I find some of it extremely confusing and rely on outside resources most of the time.
#8
Re: Simple text file
Posted 16 March 2013 - 02:22 PM
Zilna, on 16 March 2013 - 07:31 AM, said:
Also, is an Excel file considered a simple text file or should it be an actual .txt file ?
An Excel file is not a simple text file, but if you create a .txt or .csv file it is easy to open it in Excel.
Your code works for me but then I have access/permission to write to the C drive. Create a subfolder of the C-drive, or store the file in your Documents folder. I wouldn't use the Desktop as it is an awkward beast. This is useful for testing but is not a long-term solution, as raised by AdamSpeight2008.
There is a Tutorials link at the top of this page that you may wish to explore.
You should always supplement your studies with reference to MSDN - make this your second home

#9
Re: Simple text file
Posted 16 March 2013 - 03:09 PM
Hello Zilna,
In addition to what others have said about saving the file in another place. You can use the (SpecialDirectories) class to get the path to My Documents. You can also get other directories. Check out the SpecialDirectories Class .
In addition to what others have said about saving the file in another place. You can use the (SpecialDirectories) class to get the path to My Documents. You can also get other directories. Check out the SpecialDirectories Class .
Dim mypath As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
Page 1 of 1