我是靠谱客的博主 危机睫毛,这篇文章主要介绍c# 配置文件App.config操作类库的方法,现在分享给大家,希望可以做个参考。

实例如下:

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#内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(189)

评论列表共有 0 条评论

立即
投稿
返回
顶部