一共有TextBox button ComboBox三個控件 、在Textbox里輸入內(nèi)容、點添加 、就把內(nèi)容添加到了ComboBox中 、 內(nèi)容需要寫入到XML或者記事本、下次重啟軟件的時候添加的內(nèi)容才會存在 、請問我該如何把我輸入的內(nèi)容添加到XML或者記事本中?
隨便寫了個代碼,通過記錄文本實現(xiàn)的。
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("請輸入內(nèi)容");
}
}
}
}