写入一个文本文件 [英] Write to a text file

查看:117
本文介绍了写入一个文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一种方式来写C#中的文本文件。
我已创建了具有名字,姓氏,电话号码,出生日期文本框的形式。
当用户点击我想这些信息按钮写了一个文本文件。这些例子我发现真的不知道我怎么了。所以这就是为什么我问放在这里

I am looking for a way to write to a text file in C#. I have created a form that has a textbox for firstname, lastname, phone number, date of birth. When a user hits the button I would like that info wrote out to a text file. The examples I have found don't really tell me how. So that's why I am asking on here.

推荐答案

很简单的方法是使用的 File.WriteAllText 。建文成单个字符串但是你想,然后用

The very simplest way is just to use File.WriteAllText. Build the text into a single string however you want to, then use

File.WriteAllText(filename, text);



另外,您可以打开一个的TextWriter 的文件做一点一滴:

using (TextWriter writer = File.CreateText(filename)) // Or AppendText
{
    writer.WriteLine("First name: {0}", firstNameInput.Text);
    writer.WriteLine("Last name: {0}", lastNameInput.Text);
    writer.WriteLine("Phone number: {0}", phoneInput.Text);
    writer.WriteLine("Date of birth: {0}", birthInput.Text);
}

请注意,您可能需要更狡猾约出生不仅仅是日期直接从用户倾销文本 - 你可能要验证它,并将其写入标准格式,例如:

Note that you may want to be more cunning about the date of birth than just dumping the text directly from the user - you may want to validate it and write it in a standard format, for example.

这篇关于写入一个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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