当前位置: 首页 > 新闻动态 > 软件编程

C#实现appSettings节点读取与修改的方法

作者:用户投稿 浏览: 发布日期:2026-01-16
[导读]:这篇文章主要介绍了C#实现appSettings节点读取与修改的方法,是非常实用的技巧,需要的朋友可以参考下

本文实例讲述了C#实现appSettings节点读取与修改的方法,分享给大家供大家参考。具体方法如下:

关键代码如下:

复制代码 代码如下:public static string GetAppSettingsValue(string key)
{
    ConfigurationManager.RefreshSection("appSettings");
    return ConfigurationManager.AppSettings[key] ?? string.Empty;
}
public static bool UpdateAppSettings(string key, string value)
{
    var _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    if (!_config.HasFile)
    {
 throw new ArgumentException("程序配置文件缺失!");
    }
    KeyValueConfigurationElement _key = _config.AppSettings.Settings[key];
    if (_key == null)
 _config.AppSettings.Settings.Add(key, value);
    else
 _config.AppSettings.Settings[key].Value = value;
    _config.Save(ConfigurationSaveMode.Modified);
    return true;
}

希望本文所述对大家的C#程序设计有所帮助。

免责声明:转载请注明出处:http://m.lexweb.cn/news/381723.html

扫一扫高效沟通

多一份参考总有益处

免费领取网站策划SEO优化策划方案

请填写下方表单,我们会尽快与您联系
感谢您的咨询,我们会尽快给您回复!