Rama AbdelRahman
Rama AbdelRahman

Reputation: 87

writing from textbox to text file C#

I want to write from textboxes in windows form to textfile, I want to use the spilt. I want to be written in the text file like this

Namelabel : NametextBox
passlabel : PasstextBox

how can I do that!

StreamWriter txt = new StreamWriter("D:\\Register.txt")

            txt.Write(Namelabel.Text);
            txt.WriteLine(NametextBox.Text);
            txt.Write(passlabel.Text);
            txt.WriteLine(PasstextBox.Text);

Upvotes: 0

Views: 1610

Answers (2)

Reza Aghaei
Reza Aghaei

Reputation: 125197

You can simply use System.IO.WriteAllText to write text in file. For example:

System.IO.File.WriteAllText("D:\\Register.txt", 
    string.Format("{0}:{1}\n{2}:{3}"
        Namelabel.Text,
        NametextBox.Text
        passlabel.Text
        PasstextBox.Text));

Upvotes: 2

Kvam
Kvam

Reputation: 2218

You're almost there:

using (StreamWriter txt = new StreamWriter("D:\\Register.txt"))
{
    txt.Write(Namelabel.Text);
    txt.WriteLine(NametextBox.Text);
    txt.Write(passlabel.Text);
    txt.WriteLine(PasstextBox.Text);
}

You might want to make it a bit easier to read by using string.Format:

using (StreamWriter txt = new StreamWriter("D:\\Register.txt"))
{
    txt.WriteLine(string.Format("{0}: {1}", Namelabel.Text, NametextBox.Text));
    txt.WriteLine(string.Format("{0}: {1}", passlabel.PasstextBox, NametextBox.Text));
}

Upvotes: 3

Related Questions