C# help about multi save the txt file in different textbox

longcrystal

Joined Mar 22, 2010
20
hi , sorry for disturb everyone , i just quite beginner in C# i have a question about save file function in textbox

here is frame picture:

here is my code but it is no completed code but i can success save one textbox in .txt file ~but i couldnt save two textbox" word" in txt file anyone got idea how to save the two textbox message in one txt file , after that i want to try read it,. now i tried to use write function into txt file .

Rich (BB code):
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string path = @"c:\book\New Text Document.txt";

private void button1_Click(object sender, EventArgs e)
{

File.WriteAllText(path, textBox1.Text);

File.WriteAllText(path, textBox2.Text);
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
this.Text = textBox1.Text;
}

private void textBox2_TextChanged(object sender, EventArgs e)
{
this.Text = textBox2.Text;
}
}
}

Last edited by a moderator:

cheezewizz

Joined Apr 16, 2009
82
on the msdn page for System.IO.File.WriteAllText it clearly states "Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten." so basically your second call to that method is overwriting the contents written by the first one. try AppendAllText instead.

longcrystal

Joined Mar 22, 2010
20
yeah i know write like this sure is overwritten because is no doing condition loop ~~so may i use the function of append text???~~~maybe i need to search these code information ~~thank for comment ~~~

i looking the effect it is write different word in two textbox after save into textfile can be view what i write into textboxt message

longcrystal

Joined Mar 22, 2010
20
well i solved the problem what i want thank for your idea information

File.WriteAllText(path, textBox1.Text);

File.AppendAllText(path, textBox2.Text);

now can be continue ~

debjit625

Joined Apr 17, 2010
790
I use Stream classes to do all these operations...

StreamWriter sw = newStreamWriter(path);
sw.WriteLine(textBox1.Text);
sw.WriteLine(textBox2.Text);
sw.Close();
Good Luck

Last edited: