C# 配置文件Config操作大全

配置文件:与应用程序一起打包,是对应用程序的配置,方便管理。

一、基本操作,读写appSettings:

        /// <summary>
        /// 添加appsettings,如果appKey不存在则添加,存在则更新
        /// </summary>
        /// <param name="appKey"></param>
        /// <param name="appValue"></param>
        public static void AddConfig(string appKey, string appValue)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (ConfigurationManager.AppSettings[appKey] == null)
                config.AppSettings.Settings.Add(appKey, appValue);
            else
                config.AppSettings.Settings[appKey].Value = appValue;

            config.Save();
            ConfigurationManager.RefreshSection("appSettings");
        }

        /// <summary>
        /// 更新指定appKey的appsettings
        /// </summary>
        /// <param name="appKey"></param>
        /// <param name="appValue"></param>
        public static void UpdateConfig(string appKey, string appValue)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings[appKey].Value = appValue;
            config.Save();  //ConfigurationSaveMode.Full
            ConfigurationManager.RefreshSection("appSettings");  //刷新命名节,在下次检索它时将从磁盘重新读取它。记住应用程序要刷新节点
        }


        /// <summary>
        /// 读取指定appKey的appsettings
        /// </summary>
        /// <param name="appKey"></param>
        /// <returns></returns>
        public static string ReadConfig(string appKey)
        {

            string read = ConfigurationManager.AppSettings[appKey];
            return read;
        }


        /// <summary>
        /// 删除指定appKey的appsettings
        /// </summary>
        /// <param name="appKey"></param>
        public static void DeleteConfig(string appKey)
        {
            Configuration config=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (ConfigurationManager.AppSettings[appKey] != null)
            {
                config.AppSettings.Settings.Remove(appKey);
                config.Save();
                ConfigurationManager.RefreshSection("appSettings");
            }
        }

二、自定义操作

1、新建section类

  class ConfigSectionData : ConfigurationSection
    {
        [ConfigurationProperty("key")]
        public string Key
        {
            get { return (string)this["key"]; }
            set { this["key"] = value; }
        }

        [ConfigurationProperty("vaule")]
        public string Value
        {
            get { return (string)this["vaule"]; }
            set { this["vaule"] = value; }
        }
    }

2、编写调用函数

         /// <summary>
        /// 读配置节点组value
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string ReadConfigGropValue(string sectionGroup, string name)
        {
            
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.GetSectionGroup(sectionGroup)!=null)
            {
                if (config.SectionGroups[sectionGroup].Sections[name] != null)
                {
                    ConfigSectionData ReadGroupValue = config.SectionGroups[sectionGroup].Sections[name] as ConfigSectionData;
                    return ReadGroupValue.Value;
                }

            }
            return null;

        }

        /// <summary>
        /// 根据index读配置节点组value
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string ReadConfigGropValue(string sectionGroup, int index)
        {

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.GetSectionGroup(sectionGroup) != null)
            {
                ConfigSectionData ReadGroupValue = config.SectionGroups[sectionGroup].Sections[index] as ConfigSectionData;
                return ReadGroupValue.Value;
            }
            return null;

        }

        /// <summary>
        /// 读配置节点组key
        /// </summary>
        /// <param name="sectionGroup"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string ReadConfigGropKey(string sectionGroup, string name)
        {

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.GetSectionGroup(sectionGroup) != null)
            {
                if (config.SectionGroups[sectionGroup].Sections[name]!=null)
                {
                    ConfigSectionData ReadGroupValue = config.SectionGroups[sectionGroup].Sections[name] as ConfigSectionData;
                    return ReadGroupValue.Key;
                }

            }
            return null;

        }


        /// <summary>
        /// 根据Index读配置节点组key
        /// </summary>
        /// <param name="sectionGroup"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string ReadConfigGropKey(string sectionGroup, int index)
        {

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.GetSectionGroup(sectionGroup) != null)
            {
                ConfigSectionData ReadGroupValue = config.SectionGroups[sectionGroup].Sections[index] as ConfigSectionData;
                return ReadGroupValue.Key;
            }
            return null;

        }



        /// <summary>
        /// 向配置节点组中(Settings)添加配置节点
        /// </summary>
        /// <param name="name"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void AddConfigGroup(string sectionGroup, string name, string key, string value)
        {
            int count = 0;
            List<string> sectionName = new List<string>();
            NameValueCollection nameCollection = new NameValueCollection();
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.SectionGroups[sectionGroup] != null)
            {
                
                count = config.SectionGroups[sectionGroup].Sections.Count;
                for (int i = 0; i < count; i++)
                {
                    ConfigSectionData temp = config.SectionGroups[sectionGroup].Sections[i] as ConfigSectionData;
                    sectionName.Add(config.SectionGroups[sectionGroup].Sections[i].SectionInformation.Name);
                    nameCollection.Add(temp.Key, temp.Value);
                }

                config.SectionGroups.Remove(sectionGroup);
                config.Save();

            }

            config.SectionGroups.Add(sectionGroup, new ConfigurationSectionGroup());

            for (int i = 0; i < count; i++)
            {
                ConfigSectionData da = new ConfigSectionData();
                da.Key = nameCollection.GetKey(i);
                da.Value = nameCollection.GetValues(i)[0];
                config.SectionGroups[sectionGroup].Sections.Add(sectionName[i], da);
                config.Save(ConfigurationSaveMode.Minimal);
            }
            if (!sectionName.Contains(name))
            {
                ConfigSectionData da1 = new ConfigSectionData();
                da1.Key = key;
                da1.Value = value;
                config.SectionGroups[sectionGroup].Sections.Add(name, da1);

            }

            config.Save(ConfigurationSaveMode.Minimal);
            ConfigurationManager.RefreshSection(sectionGroup);

        }


        /// <summary>
        /// 读取节点组中secton个数
        /// </summary>
        public static int  ReadGroupSectonCount(string sectionGroup)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.SectionGroups[sectionGroup] != null)
            {

               int  count = config.SectionGroups[sectionGroup].Sections.Count;
                return count;
            }
            else
            {
                return 0;
            }

        }


        /// <summary>
        /// 更新配置节点组中配置节点
        /// </summary>
        /// <param name="name"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>

        public static void UpdateConfigGroup(string sectionGroup, string name, string key, string value)
        {
            int count = 0;
            List<string> sectionName = new List<string>();
            NameValueCollection nameCollection = new NameValueCollection();
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (config.SectionGroups[sectionGroup] != null)
            {
                count = config.SectionGroups[sectionGroup].Sections.Count;
                for (int i = 0; i < count; i++)
                {
                    ConfigSectionData temp = config.SectionGroups[sectionGroup].Sections[i] as ConfigSectionData;
                    sectionName.Add(config.SectionGroups[sectionGroup].Sections[i].SectionInformation.Name);
                    nameCollection.Add(temp.Key, temp.Value);
                }
                config.SectionGroups.Remove(sectionGroup);
                config.Save();

            }
            else
            {
                config.SectionGroups.Add(sectionGroup, new ConfigurationSectionGroup());
                ConfigSectionData da = new ConfigSectionData();
                da.Key = key;
                da.Value = value;
                config.SectionGroups[sectionGroup].Sections.Add(name, da);
                config.Save(ConfigurationSaveMode.Minimal);
                count = 0;
                config.Save(ConfigurationSaveMode.Minimal);
                ConfigurationManager.RefreshSection(sectionGroup);
                return;
            }

            config.SectionGroups.Add(sectionGroup, new ConfigurationSectionGroup());
            for (int i = 0; i < count; i++)
            {
 
                if (sectionName[i] == name)
                {
                    ConfigSectionData da = new ConfigSectionData();
                    da.Key = key;
                    da.Value = value;
                    config.SectionGroups[sectionGroup].Sections.Add(name, da);
                    config.Save(ConfigurationSaveMode.Minimal);
                }
                else
                {
                    ConfigSectionData da = new ConfigSectionData();
                    da.Key = nameCollection.GetKey(i);
                    da.Value = nameCollection.GetValues(i)[0];
                    config.SectionGroups[sectionGroup].Sections.Add(sectionName[i], da);
                    config.Save(ConfigurationSaveMode.Minimal);
                }
            }

            if (!sectionName.Contains(name))
            {
                ConfigSectionData da = new ConfigSectionData();
                da.Key = key;
                da.Value = value;
                config.SectionGroups[sectionGroup].Sections.Add(name, da);
                config.Save(ConfigurationSaveMode.Minimal);
            }

            config.Save(ConfigurationSaveMode.Minimal);
            ConfigurationManager.RefreshSection(sectionGroup);
        }

        /// <summary>
        /// 更新配置节点组中配置节点key,key和name绑定
        /// </summary>
        /// <param name="name"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>

        public static void UpdateConfigGroupKey(string sectionGroup, string name, string key)
        {
            int count = 0;
            List<string> sectionName = new List<string>();
            NameValueCollection nameCollection = new NameValueCollection();
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (config.SectionGroups[sectionGroup] != null)
            {
                count = config.SectionGroups[sectionGroup].Sections.Count;
                for (int i = 0; i < count; i++)
                {
                    ConfigSectionData temp = config.SectionGroups[sectionGroup].Sections[i] as ConfigSectionData;
                    sectionName.Add(config.SectionGroups[sectionGroup].Sections[i].SectionInformation.Name);
                    nameCollection.Add(temp.Key, temp.Value);
                }
                config.SectionGroups.Remove(sectionGroup);
                config.Save();

            }

            config.SectionGroups.Add(sectionGroup, new ConfigurationSectionGroup());
            for (int i = 0; i < count; i++)
            {
                if (sectionName[i] == name)
                {
                    ConfigSectionData da = new ConfigSectionData();
                    da.Key = key;
                    da.Value = nameCollection.GetValues(i)[0];
                    config.SectionGroups[sectionGroup].Sections.Add(key, da);
                    config.Save(ConfigurationSaveMode.Minimal);
                }
                else
                {
                    ConfigSectionData da = new ConfigSectionData();
                    da.Key = nameCollection.GetKey(i);
                    da.Value = nameCollection.GetValues(i)[0];
                    config.SectionGroups[sectionGroup].Sections.Add(sectionName[i], da);
                    config.Save(ConfigurationSaveMode.Minimal);
                }
            }


            config.Save(ConfigurationSaveMode.Minimal);
            ConfigurationManager.RefreshSection(sectionGroup);
        }


        /// <summary>
        /// 更新配置节点组中配置节点Value
        /// </summary>
        /// <param name="name"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>

        public static void UpdateConfigGroupValue(string sectionGroup, string name, string value)
        {
            int count = 0;
            List<string> sectionName = new List<string>();
            NameValueCollection nameCollection = new NameValueCollection();
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (config.SectionGroups[sectionGroup] != null)
            {
                count = config.SectionGroups[sectionGroup].Sections.Count;
                for (int i = 0; i < count; i++)
                {
                    ConfigSectionData temp = config.SectionGroups[sectionGroup].Sections[i] as ConfigSectionData;
                    sectionName.Add( config.SectionGroups[sectionGroup].Sections[i].SectionInformation.Name);
                    nameCollection.Add(temp.Key, temp.Value);
                }
                config.SectionGroups.Remove(sectionGroup);
                config.Save();

            }


            config.SectionGroups.Add(sectionGroup, new ConfigurationSectionGroup());
            for (int i = 0; i < count; i++)
            {
                if (sectionName[i] == name)
                {
                    ConfigSectionData da = new ConfigSectionData();
                    da.Key = nameCollection.GetKey(i);
                    da.Value = value;
                    config.SectionGroups[sectionGroup].Sections.Add(name, da);
                    config.Save(ConfigurationSaveMode.Minimal);
                }
                else
                {
                    ConfigSectionData da = new ConfigSectionData();
                    da.Key = nameCollection.GetKey(i);
                    da.Value = nameCollection.GetValues(i)[0];
                    config.SectionGroups[sectionGroup].Sections.Add(sectionName[i], da);
                    config.Save(ConfigurationSaveMode.Minimal);
                }
            }


            config.Save(ConfigurationSaveMode.Minimal);
            ConfigurationManager.RefreshSection(sectionGroup);
        }


        /// <summary>
        /// 更新配置节点组名
        /// </summary>
        /// <param name="name"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>

        public static void UpdateConfigGroup(string sectionGroup, string newSectionGroup)
        {
            int count = 0;
            List<string> sectionName = new List<string>();
            NameValueCollection nameCollection = new NameValueCollection();
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (config.SectionGroups[sectionGroup] != null)
            {
                count = config.SectionGroups[sectionGroup].Sections.Count;
                for (int i = 0; i < count; i++)
                {
                    ConfigSectionData temp = config.SectionGroups[sectionGroup].Sections[i] as ConfigSectionData;
                    sectionName.Add(config.SectionGroups[sectionGroup].Sections[i].SectionInformation.Name);
                    nameCollection.Add(temp.Key, temp.Value);
                }
                config.SectionGroups.Remove(sectionGroup);
                config.Save();

            }

            config.SectionGroups.Add(newSectionGroup, new ConfigurationSectionGroup());
            for (int i = 0; i < count; i++)
            {

                    ConfigSectionData da = new ConfigSectionData();
                    da.Key = nameCollection.GetKey(i);
                    da.Value = nameCollection.GetValues(i)[0];
                    config.SectionGroups[newSectionGroup].Sections.Add(sectionName[i], da);
                    config.Save(ConfigurationSaveMode.Minimal);               
            }


            config.Save(ConfigurationSaveMode.Minimal);
            ConfigurationManager.RefreshSection(newSectionGroup);
        }

        /// <summary>
        /// 删除配置节点组中(Settings)配置节点
        /// </summary>
        /// <param name="name"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void DeleteGroupSection(string sectionGroup,string name)
        {

            int count = 0;
            List<string> sectionName = new List<string>();
            NameValueCollection nameCollection = new NameValueCollection();
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);


            if (config.SectionGroups[sectionGroup] != null)
            {

                count = config.SectionGroups[sectionGroup].Sections.Count;
                for (int i = 0; i < count; i++)
                {
                    ConfigSectionData temp = config.SectionGroups[sectionGroup].Sections[i] as ConfigSectionData;
                    sectionName.Add(config.SectionGroups[sectionGroup].Sections[i].SectionInformation.Name);
                    nameCollection.Add(temp.Key, temp.Value);
                }
                config.SectionGroups.Remove(sectionGroup);
                config.Save();

            }

            config.SectionGroups.Add(sectionGroup, new ConfigurationSectionGroup());
            for (int i = 0; i < count; i++)
            {
                if (sectionName[i] != name)
                {
                    ConfigSectionData da = new ConfigSectionData();
                    da.Key = nameCollection.GetKey(i);
                    da.Value = nameCollection.GetValues(i)[0];
                    config.SectionGroups[sectionGroup].Sections.Add(sectionName[i], da);
                    config.Save(ConfigurationSaveMode.Minimal);
                }
            }


            config.Save(ConfigurationSaveMode.Minimal);
            ConfigurationManager.RefreshSection(sectionGroup);
        }


        /// <summary>
        /// 删除配置节点组
        /// </summary>
        public static void DeleteConfigGroup(string sectionGroup)
        {

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.SectionGroups.Remove(sectionGroup);
            //config.SectionGroups.Clear();
            config.Save(ConfigurationSaveMode.Minimal);

        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无熵~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值