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

Discussion in 'Programmer's Corner' started by longcrystal, Dec 15, 2011.

  1. longcrystal

    Thread Starter New Member

    Mar 22, 2010
    20
    0
    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:
    [​IMG]

    Uploaded with ImageShack.us

    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 .

    Code ( (Unknown Language)):
    1.  
    2. using System;
    3. using System.IO;
    4. using System.Collections.Generic;
    5. using System.ComponentModel;
    6. using System.Data;
    7. using System.Drawing;
    8. using System.Linq;
    9. using System.Text;
    10. using System.Windows.Forms;
    11.  
    12. namespace WindowsFormsApplication1
    13. {
    14.     public partial class Form1 : Form
    15.     {
    16.         public Form1()
    17.         {
    18.             InitializeComponent();
    19.         }
    20.         string path = @"c:\book\New Text Document.txt";
    21.  
    22.         private void button1_Click(object sender, EventArgs e)
    23.         {
    24.          
    25.          
    26.                 File.WriteAllText(path, textBox1.Text);
    27.            
    28.                 File.WriteAllText(path, textBox2.Text);
    29.         }
    30.  
    31.        
    32.  
    33.         private void textBox1_TextChanged(object sender, EventArgs e)
    34.         {
    35.             this.Text = textBox1.Text;
    36.         }
    37.  
    38.         private void textBox2_TextChanged(object sender, EventArgs e)
    39.         {
    40.             this.Text = textBox2.Text;
    41.         }
    42.     }
    43. }
    44.  
     
    Last edited by a moderator: Dec 15, 2011
  2. cheezewizz

    Active Member

    Apr 16, 2009
    82
    10
    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.
     
  3. longcrystal

    Thread Starter New Member

    Mar 22, 2010
    20
    0
    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
     
  4. longcrystal

    Thread Starter New Member

    Mar 22, 2010
    20
    0
    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 ~
     
  5. debjit625

    Well-Known Member

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

    Good Luck
     
    Last edited: Dec 15, 2011
Loading...