一共有TextBox button ComboBox三个控件 、在Textbox里输入内容、点添加 、就把内容添加到了ComboBox中 、 内容需要写入到XML或者记事本、下次重启软件的时候添加的内容才会存在 、请问我该如何把我输入的内容添加到XML或者记事本中?
随便写了个代码,通过记录文本实现的。
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 WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
FileInfo DataFile;
DataFile = new FileInfo(@"c:\2.txt");
if (DataFile.Exists)
{
StreamReader sr = new StreamReader(@"c:\2.txt", Encoding.GetEncoding("GB2312"), true);
string line;
while ((line = sr.ReadLine()) != null)
{
comboBox1.Items.Add(line);
}
sr.Close();
}
else
{
DataFile.Create();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
GC.Collect();
StreamWriter writeline = new StreamWriter(@"c:\2.txt", true, Encoding.GetEncoding("GB2312"));
writeline.Write(textBox1.Text + "\r\n");
writeline.Flush();
writeline.Close();
comboBox1.Items.Add(textBox1.Text);
textBox1.Text = "";
}
else
{
MessageBox.Show("请输入内容");
}
}
}
}