从文本框写入文本文件C# [英] writting from textbox to text file C#

查看:113
本文介绍了从文本框写入文本文件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);

推荐答案

问题在于你根本没有写冒号,或者在将数据写入文件时向数据添加新行。试试这个,



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,

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





你所缺少的是你只是在没有遵循格式的情况下编写所有内容。在模板中,您使用冒号和新行。如果您可能想要使用模板,我建议使用 String.Format() [ ^ ]构建要保存的数据样本的方法。



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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆