如何将Windows窗体中输入的文本保存到文本文件中?数据不会附加在文本文件中 [英] how do I save the text entered in a windows form to a text file? data is nt appending in text file
本文介绍了如何将Windows窗体中输入的文本保存到文本文件中?数据不会附加在文本文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试开发一个Windows窗体应用程序,它接受输入数据,如在文本框中输入的数据,选择的单选按钮等,并将该数据保存到文本文件中。它工作但数据没有附加在该文件中,它正在替换。
I am trying to develop an windows form application which accepts the input data like the data entered in textboxes, radio buttons selected etc., and save that data into a text file. It worked but the data is not appending in that file,it is replacing.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace fb
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
StreamWriter ws = new StreamWriter("C:\\File.txt");
//sw.Write(String.Join(Environment.NewLine, listBox1.Items));
//textbox
ws.Write(String.Join(Environment.NewLine,"Name : "));
ws.WriteLine(String.Join(Environment.NewLine,textBox1.Text));
ws.WriteLine(String.Join(Environment.NewLine,"Responses are "));
//q1
ws.WriteLine(String.Join(Environment.NewLine,"For q1 : "));
if (radioButton1.Checked)
{
ws.Write(String.Join(Environment.NewLine,radioButton1.Text));
}
else
{
ws.Write(String.Join(Environment.NewLine,radioButton2.Text));
}
ws.WriteLine(String.Join(Environment.NewLine,"\n"));
//q2
ws.WriteLine(String.Join(Environment.NewLine,"For q2"));
if (radioButton3.Checked)
{
ws.Write(String.Join(Environment.NewLine,radioButton3.Text));
}
else if (radioButton4.Checked)
{
ws.Write(String.Join(Environment.NewLine,radioButton4.Text));
}
else
{
ws.Write(String.Join(Environment.NewLine,radioButton5.Text));
}
ws.WriteLine(String.Join(Environment.NewLine,"\n"));
//saving
ws.Close();
MessageBox.Show("saved.");
}
}
}
这里是截图:
http://s0.uploads.im/QLZ7W.jpg [ ^ ]
推荐答案
使用文件.AppendText :
Use File.AppendText:
StreamWriter ws = File.AppendText("C:\\File.txt");
或
使用 StreamWriter构造函数 [ ^ ]:
or
Use another overload of the StreamWriter constructor[^]:
StreamWriter ws = new StreamWriter("C:\\File.txt", true);
这篇关于如何将Windows窗体中输入的文本保存到文本文件中?数据不会附加在文本文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文