C#将数据写入文本文件 [英] C# Write Data to a text File

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

问题描述

我在这里得到了家庭作业。我创建了一个带有4个文本框的表单,用于输入数据,账号,名字,姓氏,余额。我有四个按钮,创建文件,将数据保存到文件,清除和退出。基本上所有的程序都是创建一个文本文件,然后我输入我的数据到文本框中,然后我点击保存数据文件,将数据写入我创建的文本文件。 清除和退出已经完成了,而且我的程序在创建文本文件的过程中起作用,现在我只需要有人指示我如何正确写入我输入到文本文件中的数据。这里是我的代码,提前致谢

  using System; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.IO;

namespace Chapter_17_Ex.Sample_1
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent ();


private void btnCreate_Click(object sender,EventArgs e)
{
SaveFileDialog file = new SaveFileDialog();
file.FileName =client.txt;
file.Filter =txt文件(* .txt)| * .txt |所有文件(*。*)| *。*;
file.ShowDialog();

StreamWriter filewrite = new StreamWriter(file.FileName);





private void btnSave_Click(object sender,EventArgs e)
{
TextWriter file = new TextWriter

}

private void btnClear_Click(object sender,EventArgs e)
{
txtAccount.Clear();
txtBalance.Clear();
txtFirstName.Clear();
txtLastName.Clear();


private void btnExit_Click(object sender,EventArgs e)
{
Application.Exit();




解决方案

您正在创建StreamWriter实例的正确轨道上。你现在要做的是使用该类的 WriteLine()方法。使用块封装StreamWriter实例在中也是一个好主意:

  private void btnCreate_Click(object sender,EventArgs e)
{
SaveFileDialog file = new SaveFileDialog();
file.FileName =client.txt;
file.Filter =txt文件(* .txt)| * .txt |所有文件(*。*)| *。*;
file.ShowDialog();使用(StreamWriter filewrite = new StreamWriter(file.FileName))

filewrite.WriteLine(String.Format(First Name is {0},txtFirstName.Text ));
//根据需要再次使用Write()或WriteLine()。
}
}


I got a homework assignment here. I've created a form with 4 text boxes for data entry, Account #, First Name, Last Name, Balance. I have four buttons, Create File, Save data to file, clear, and exit. Basically all the program does is create a text file, then I input my data into the text boxes, then I hit save data file which will write the data to the text file I have created. Clear and Exit are already done, and I have the program working as far as creating the text file, now I just need someone to point me in the right direction on how to actually write the data I entered into the text file. Here is my code, Thanks in advance

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Chapter_17_Ex.Sample_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            SaveFileDialog file = new SaveFileDialog();
            file.FileName = "client.txt";
            file.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            file.ShowDialog();           

            StreamWriter filewrite = new StreamWriter(file.FileName);          



        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            TextWriter file = new TextWriter

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtAccount.Clear();
            txtBalance.Clear();
            txtFirstName.Clear();
            txtLastName.Clear();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

解决方案

You're on the right track with creating that StreamWriter instance. What you want to do now is use the WriteLine() method of that class. It's also a good idea to wrap that StreamWriter instance in a using block:

    private void btnCreate_Click(object sender, EventArgs e)
            {
                SaveFileDialog file = new SaveFileDialog();
                file.FileName = "client.txt";
                file.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                file.ShowDialog();           

                using(StreamWriter filewrite = new StreamWriter(file.FileName))
                {
                     filewrite.WriteLine( String.Format("First Name is {0}", txtFirstName.Text)  );
                     //use Write() or WriteLine() again as needed.
                }
            }

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

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