c# 配置文件App.config *** 作类库的方法

c# 配置文件App.config *** 作类库的方法,第1张

概述实例如下:publicclassConfigOperator{#region从配置文件获取Value///<summary>///从配置文件获取Value

实例如下:

public class ConfigOperator  {    #region 从配置文件获取Value    /// <summary>    /// 从配置文件获取Value    /// </summary>    /// <param name="key">配置文件中key字符串</param>    /// <returns></returns>    public static string GetValueFromConfig(string key)    {      try      {        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);        //获取AppSettings的节点         AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");        return appsection.Settings[key].Value;      }      catch      {        return "";      }    }    #endregion    #region 设置配置文件    /// <summary>    /// 设置配置文件    /// </summary>    /// <param name="key">配置文件中key字符串</param>    /// <param name="value">配置文件中value字符串</param>    /// <returns></returns>    public static bool SetValueFromConfig(string key,string value)    {      try      {        //打开配置文件         Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);        //获取AppSettings的节点         AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");        appsection.Settings[key].Value = value;        config.Save();        return true;      }      catch      {        return false;      }    }    #endregion

以上这篇c# 配置文件App.config *** 作类库的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的c# 配置文件App.config *** 作类库的方法全部内容,希望文章能够帮你解决c# 配置文件App.config *** 作类库的方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1256984.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存