本文介绍了从文本框写入文本文件C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从windows窗体中的文本框写入textfile,我想使用spilled。我想写在这样的文本文件中
Namelabel:NametextBox
passlabel:PasstextBox
我该怎么做!
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);
推荐答案
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.
这篇关于从文本框写入文本文件C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!