//写配置文件:不存在则增加,存在则修改
private void SaveLastSearchTemplate()
{ if (cbbSearchTemplate.SelectedIndex > -1) { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (config.AppSettings.Settings["SearchCondition.LastUsed"] == null) config.AppSettings.Settings.Add("SearchCondition.LastUsed", cbbSearchTemplate.SelectedIndex.ToString()); else config.AppSettings.Settings["SearchCondition.LastUsed"].Value = cbbSearchTemplate.SelectedIndex.ToString(); config.Save(ConfigurationSaveMode.Modified); } }
//读配置文件
private void LoadLastSearchTemplate() { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (config.AppSettings.Settings["SearchCondition.LastUsed"] != null) { int lastIndex = int.Parse(config.AppSettings.Settings["SearchCondition.LastUsed"].Value); if (cbbSearchTemplate.Items.Count > lastIndex) cbbSearchTemplate.SelectedIndex = lastIndex; } }
//备注:在IDE中debug时,貌似无法写入,反正我这里调试是没有任何反应的