Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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!

C#
StreamWriter txt = new StreamWriter("D:\\Register.txt")
            
            txt.Write(Namelabel.Text);
            txt.WriteLine(NametextBox.Text);
            txt.Write(passlabel.Text);
            txt.WriteLine(PasstextBox.Text);
Posted

1 solution

The problem is that you are not at all writing the colons, or adding a new line to the data when you write it to the file. Try this,

C#
string data = string.Format("{0} : {1}\n{2} : {3}", 
                            Namelabel.Text, 
                            Nametextbox.Text,
                            passlabel.Text, 
                            Passtextbox.Text
               );
txt.Write(data);


What you were missing was that you were simply writing everything without a format being followed. In your template, you are using colons and a new line. In cases where you might want to use a template, I would recommend using a String.Format()[^] method to build the data sample to be saved.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Nov-15 15:33pm    
5ed.
—SA
Afzaal Ahmad Zeeshan 1-Nov-15 15:34pm    
Thank you, Sergey.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900