1.. 成员变量
private string baseDir = AppDomain.CurrentDomain.BaseDirectory;
private string hisFileName;
2. Load
//configFilename
hisFileName = baseDir + "importConfig.txt";
string txt = null, newTxt = null;
if (File.Exists(hisFileName))
{
using (StreamReader sr = new StreamReader(hisFileName, Encoding.UTF8))
{
do
{
newTxt = sr.ReadLine();
txt = newTxt != null ? newTxt : txt;
if (null != newTxt)
comboBox2.Items.Add(newTxt);
} while (null != newTxt);
}
if (null != txt && txt.Length > 0)
{
txtFilename.Text = txt;
UpdateCombox();
}
}
3. 按钮
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "导入配置文件(*.xml)|*.xml";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtFilename.Text = ofd.FileName;
UpdateCombox();
//写入历史记录
bool flag = true;
string txt = null;
using (StreamReader sr = new StreamReader(hisFileName, Encoding.UTF8))
{
while (null != (txt = sr.ReadLine()))
{
if (txt.Equals(txtFilename.Text))
{
flag = false;
break;
}
}
}
if (flag)
{
using (StreamWriter sw = new StreamWriter(hisFileName, true))
{
sw.WriteLine(txtFilename.Text);
sw.Flush();
}
}
4. 组合框
if (-1 != comboBox2.SelectedIndex)
{
txtFilename.Text = comboBox2.Items[comboBox2.SelectedIndex].ToString();
UpdateCombox();
}
5. 文件编辑按钮
MyCommon.ExecuteProcess("notepad.exe", hisFileName);