tags:

views:

39

answers:

4

Hi Can someone tell me how to; work with textBox(s) and writing the information in it to a file and read them back from the file(.txt file)

Thanks.

ps: I want to write some text in textbox (winforms) and when i click button save all the texts in all textboxs write to a file

Daniel

+3  A: 

That is pretty vague, but string txt = File.ReadAllText(path); and File.WriteAllText(path,txt); should handle the file part (for moderately sized files).

Marc Gravell
+1  A: 

The .Text property of a TextBox contains the text within the text box. You can get or set this property to acquire or alter the text within the TextBox as needed. Take a look at File.WriteAllText and File.ReadAllText to read/write text from/to a file.

Will A
A: 

Write:

FileStream fs = new FileStream("test.txt", FileMode.OpenOrCreate);
        StreamWriter sw = new StreamWriter(fs);
        sw.WriteLine(txtTest.Text);
        sw.Close();

Read:

FileStream fs = new FileStream("test.txt", FileMode.OpenOrCreate);
        StreamReader sr = new StreamReader(fs);
        string myText = string.Empty;
        while (!sr.EndOfStream)
        {
            myText += sr.ReadLine();
        }
        sr.Close();
        txtTest.Text = myText;

This is what you're asking for?

Rising Force
no because i know how to write or read from file but what i don't know is i don't know how to pass textBox1.text or other textBoxs to my method in another class which i write for just work with Files on my program. tnx :)
Daniel
A: 

i got what i want here is the code (just for write):

    public partial class Form1 : Form
{
    FileProcess fileprocess;
    public Form1()
    {
        InitializeComponent();
        fileprocess = new FileProcess();
    }

    public void writeFile()
    {        
        fileprocess.writeFile(textBox1.Text,textBox2.Text);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        writeFile();
    }

}

my class for work with the file :

    class FileProcess
{
    string path = @"c:\PhoneBook\PhoneBook.txt";

    public void writeFile(string text1,string text2)
    {
        using (StreamWriter sw = new StreamWriter(path,true))
        {
            sw.WriteLine(text1);
            sw.WriteLine(text2);
        }
    }
}

my mistake was that i try to store all textBox to a string like "info" and pass it through WriteFile() method and this is where i was stuck in it.

tnx to all.

Daniel