Saving text from a textbox to a file

Trying to save it with newlines included

Page 1 of 1

6 Replies - 5694 Views - Last Post: 02 April 2009 - 04:31 PM Rate Topic: -----

#1 garrettskye  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-January 09

Saving text from a textbox to a file

Post icon  Posted 02 April 2009 - 03:08 PM

I'm trying to save the contents of a textbox to a file, with each line of the textbox on a separate line. Here's the code I have right now, but it is just adding it all to one big line.

		private void saveToolStripMenuItem_Click(object sender, EventArgs e)
		{

			FileInfo file1 = new FileInfo("c:\\file.txt");
			StreamWriter sw = file1.CreateText();
			sw.WriteLine(txtOutput.Text + System.Environment.NewLine);	
			sw.Close();
			MessageBox.Show("File saved to C:\\file.txt");
		}



I realize now that this takes all the text, then adds a newline at the end of all of it. Am I going to have to read the textbox until I find a newline then use WriteLine to save each string individually, or is there an easier solution to this that I'm not seeing?

Thanks for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: Saving text from a textbox to a file

#2 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Saving text from a textbox to a file

Posted 02 April 2009 - 03:36 PM

ok, im a little bit confused on what you are asking for. A textbox can only have one line in it. All that a text box does is take one line of text separating words by spaces and what not. If you're meaning a ListBox then you could use the following code:
StreamWriter sw = new StreamWriter("link to text file here");
foreach (object o in listBox1.Items)
{
	   sw.WriteLine(o.ToString());
}
sw.Close();


if this is not what you mean to do, could you please specify how you want a TextBox String to be saved?
Was This Post Helpful? 0
  • +
  • -

#3 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Saving text from a textbox to a file

Posted 02 April 2009 - 04:02 PM

Okay, i re-read the post and i think i know what you are asking for now. If you have a textbox with multi-line enabled and want to save each line to a text file line by line, i got the solution for you. just do the following:
		private void button2_Click(object sender, EventArgs e)
		{
			StreamWriter sw = new StreamWriter("Link to text file here");
			String[] contents = textBox1.Lines.ToArray();
			for (int i = 0; i < contents.Length; i++)
			{
				sw.WriteLine(contents[i]);
			}
			sw.Close();
		}



This will read the contents of the textbox line by line, then save it line by line to a text file ^_~ hope this is what you are looking for!
Was This Post Helpful? 0
  • +
  • -

#4 garrettskye  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-January 09

Re: Saving text from a textbox to a file

Posted 02 April 2009 - 04:05 PM

Basically, I've got a multiline textbox with a bunch of info in it, separated on different lines like this:

One Line
Two Line
Three Line

When I save that data to a file, I want the file to look like that, with each line of the textbox in an individual line in the textfile, not like this:

One Line Two Line Three Line
Was This Post Helpful? 0
  • +
  • -

#5 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Saving text from a textbox to a file

Posted 02 April 2009 - 04:08 PM

View Postgarrettskye, on 2 Apr, 2009 - 03:05 PM, said:

Basically, I've got a multiline textbox with a bunch of info in it, separated on different lines like this:

One Line
Two Line
Three Line

When I save that data to a file, I want the file to look like that, with each line of the textbox in an individual line in the textfile, not like this:

One Line Two Line Three Line


Then my last post with the array will defiantly do the trick for you! just tested it out and it works fine on my end ^_~
Was This Post Helpful? 0
  • +
  • -

#6 garrettskye  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-January 09

Re: Saving text from a textbox to a file

Posted 02 April 2009 - 04:19 PM

This worked like a charm. Helped me find another problem with my code as well. Thanks a million!
Was This Post Helpful? 0
  • +
  • -

#7 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Saving text from a textbox to a file

Posted 02 April 2009 - 04:31 PM

View Postgarrettskye, on 2 Apr, 2009 - 03:19 PM, said:

This worked like a charm. Helped me find another problem with my code as well. Thanks a million!

not a problem, anytime!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1