這裏使用ComboBox顯示和記錄瀏覽歷史,用*.ini文件永久記錄瀏覽歷史,這裏記錄的是文件夾路徑,記錄文件路徑也是同樣的。this
string inimultipleFilePath = Application.StartupPath + @"\Plugin\CreateWaterhistoricalpath.ini";//*.ini文件的存放路徑ip
List<string> listHistory = new List<string>();//用於臨時存儲瀏覽歷史string
//在初始化窗體時遍歷*.ini文件的內容,寫到ComboBox中it
if (File.Exists(inimultipleFilePath))
{
listHistory.Clear();
Console.WriteLine();
StreamReader sr = new StreamReader(inimultipleFilePath, Encoding.Default);
while (sr.Peek() >= 0)
{
string history = sr.ReadLine();
listHistory.Add(history);
comboBox.Items.Add(history);
}
sr.Close();
sr.Dispose();
}io
//記錄歷史路徑foreach
//臨時記錄路徑,由於每次記錄歷史路徑到*.ini中,ComboBox都不會實時添加,因此最好臨時給它添加一次路徑
if (!comboBox.Items.Contains(Path))
this.comboBox.Items.Add(Path);
//記錄路徑
int index = listHistory.IndexOf(comboBox.Text);
if (index < 0)
{
listHistory.Insert(0, comboBox.Text);
if (listHistory.Count > 5) listHistory.RemoveAt(5);
}
if (index > 0)
{
listHistory.RemoveAt(index);//若是瀏覽記錄已經存在,則將它置頂
listHistory.Insert(0, cmbDirectory.Text);
}
StreamWriter sw = new StreamWriter(inimultipleFilePath, false, Encoding.Default);
foreach (string history in listHistory)
{
sw.WriteLine(history);
}
sw.Close();
sw.Dispose();coding