C# 获取自定义配置文件

/// <summary>
    /// 获取自定义配置文件
    /// </summary>
    public class Config : ConfigurationSection
    {
        private Configuration configObject;
        public const string ORACLE = "oracle";
        public const string MYSQL = "mysql";
        public const string SQLSERVER = "sqlserver";

        /// <summary>
        /// 数据库连接类型
        /// </summary>
        /// <param name="dataBaseType">oracle/mysql/sqlserver</param>
        public Config(string dataBaseType)
        {
            dataBaseType = dataBaseType.ToLower();

            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = AppDomain.CurrentDomain.BaseDirectory + "ConfigFiles\\" + dataBaseType + ".config";

            configObject = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
        }

        /// <summary>
        /// 得到对应键的值
        /// </summary>
        /// <param name="key">键名</param>
        /// <returns></returns>
        public string GetValue(string key)
        {
            string val = string.Empty;

            if (configObject.AppSettings.Settings.AllKeys.Contains(key))
                val = configObject.AppSettings.Settings[key].Value;

            return val;
        }

        /// <summary>
        /// 获取所有配置文件
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> GetConfig()
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            foreach (string key in configObject.AppSettings.Settings.AllKeys)
                dict.Add(key, configObject.AppSettings.Settings[key].Value);

            return dict;
        }

        /// <summary>
        /// 根据键值获取配置文件
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public string GetConfig(string key, string defaultValue)
        {
            string val = string.Empty;

            if (configObject.AppSettings.Settings.AllKeys.Contains(key))
            {
                val = configObject.AppSettings.Settings[key].Value;
            }

            if (string.IsNullOrEmpty(val))
            {
                return defaultValue;
            }

            return val;
        }

        /// <summary>
        /// 写配置文件,如果节点不存在则自动创建
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        public bool SetConfig(string key, string value)
        {
            try
            {
                //Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                if (!configObject.AppSettings.Settings.AllKeys.Contains(key))
                    configObject.AppSettings.Settings.Add(key, value);
                else
                    configObject.AppSettings.Settings[key].Value = value;
                configObject.Save(ConfigurationSaveMode.Modified);
                //ConfigurationManager.RefreshSection("appSettings");

                return true;
            }

            catch { return false; }
        }

        /// <summary>
        /// 写配置文件(用键值创建),如果节点不存在则自动创建
        /// </summary>
        /// <param name="dict">键值集合</param>
        /// <returns></returns>
        public bool SetConfig(Dictionary<string, string> dict)
        {
            try
            {
                if (dict == null || dict.Count == 0)
                    return false;

                //Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                foreach (string key in dict.Keys)
                {
                    if (!configObject.AppSettings.Settings.AllKeys.Contains(key))
                        configObject.AppSettings.Settings.Add(key, dict[key]);
                    else
                        configObject.AppSettings.Settings[key].Value = dict[key];
                }

                configObject.Save(ConfigurationSaveMode.Modified);

                return true;
            }
            catch { return false; }
        }
    }

    public class ItemsSection : ConfigurationSection
    {
        [ConfigurationProperty("items", IsRequired = true)]
        public string Category
        {
            get
            {
                return (string)base["Category"];
            }

            set
            {
                base["Category"] = value;
            }
        }
        [ConfigurationProperty("", IsDefaultCollection = true)]
        public ItemElementCollection Books
        {
            get
            {
                return (ItemElementCollection)base[""];
            }
        }
    }

    public class ItemElementCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new ItemElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ItemElement)element).Name;
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }
        protected override string ElementName
        {
            get
            {
                return "item";
            }
        }
        public ItemElement this[int index]
        {
            get
            {
                return (ItemElement)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }

        }
    }

    public class ItemElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get
            {
                return (string)base["name"];
            }

            set
            {
                base["name"] = value;
            }
        }

        [ConfigurationProperty("icon", IsRequired = true)]

        public string Icon
        {
            get
            {
                return (string)base["icon"];
            }

            set
            {
                base["icon"] = value;
            }
        }

        [ConfigurationProperty("assembly", IsRequired = true)]
        public string Assembly
        {
            get
            {
                return (string)base["assembly"];
            }

            set
            {
                base["assembly"] = value;
            }
        }

        [ConfigurationProperty("target", IsRequired = true)]
        public string Target
        {
            get
            {
                return (string)base["target"];
            }

            set
            {
                base["target"] = value;
            }
        }

        [ConfigurationProperty("kind", IsRequired = true)]
        public string Kind
        {
            get
            {
                return (string)base["kind"];
            }

            set
            {
                base["name"] = value;
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值