C#读写配置文件

本文介绍了如何使用C#在应用程序中保存和加载搜索模板的配置信息。通过检测下拉框的选定索引并将其写入配置文件,在下次启动时能够自动加载最后一次使用的搜索模板。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//写配置文件:不存在则增加,存在则修改

        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时,貌似无法写入,反正我这里调试是没有任何反应的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值