8 Replies - 2453 Views - Last Post: 16 March 2013 - 03:09 PM Rate Topic: -----

#1 Zilna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 08-October 10

Simple text file

Posted 16 March 2013 - 07:31 AM

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 ?

Is This A Good Question/Topic? 0
  • +

Replies To: Simple text file

#2 Zilna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 08-October 10

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

Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

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.
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

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?

This post has been edited by AdamSpeight2008: 16 March 2013 - 09:01 AM

Was This Post Helpful? 0
  • +
  • -

#5 Zilna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 08-October 10

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.
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

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
  • StreamWriter is .net type.
  • using File.Append

What IDE are you using? What it say in the Help -> About window?
Was This Post Helpful? 1
  • +
  • -

#7 Zilna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 08-October 10

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.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3621
  • View blog
  • Posts: 12,510
  • Joined: 12-December 12

Re: Simple text file

Posted 16 March 2013 - 02:22 PM

View PostZilna, 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 :)
Was This Post Helpful? 1
  • +
  • -

#9 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 329
  • View blog
  • Posts: 995
  • Joined: 01-February 13

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 .
        Dim mypath As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1