zoukankan      html  css  js  c++  java
  • C# 以枚举名为Section,枚举值为 Key 读取 ini 中的Value

        /// <summary>
        /// 以枚举名称为Section,枚举值为Key的ini配置文件的读写
        /// </summary>
        public class IniFileOperate : IniRW
        {
    
            protected Dictionary<object, string> _DefaultParam = new Dictionary<object, string>();
    
            public IniFileOperate(string path)
                : base(path)
            {
            }
    
            public bool AddDefaultValue<T>(T key, string value)
            {
                _DefaultParam[key] = value;
                return true;
            }
    
            public bool WriteValue<T>(T key, object value)
            {
                string section = key.GetType().Name;
                return WriteValue(section, key.ToString(), value);
            }
    
            public string ReadString<T>(T key)
            {
                string section = key.GetType().Name;
                return ReadString(section, key.ToString());
            }
    
            public override string ReadString(string section, string key)
            {
                string ret = base.ReadString(section, key);
                if (ret == string.Empty)
                {
                    KeyValuePair<object, string> keyValue = _DefaultParam.FirstOrDefault(v => v.Key.ToString() == key);
                    if (keyValue.Key != null && keyValue.Value != null)
                    {
                        object k = keyValue.Key;
                        string value = _DefaultParam[k];
                        WriteValue(k, value);
                        ret = value;
                    }
                }
                return ret;
            }
    
            public int ReadInt<T>(T key)
            {
                return (int)ReadDouble(key);
            }
    
            public short ReadShort<T>(T key)
            {
                return (short)ReadDouble(key);
            }
    
            public byte ReadByte<T>(T key)
            {
                return (byte)ReadDouble(key);
            }
    
            public double ReadDouble<T>(T key)
            {
                string section = key.GetType().Name;
                return ReadDouble(section, key.ToString());
            }
    
            public bool ReadBool<T>(T key)
            {
                string section = key.GetType().Name;
                return ReadBool(section, key.ToString());
            }
    
            public DateTime ReadDateTime<T>(T key)
            {
                string section = key.GetType().Name;
                string time = ReadString(section, key.ToString());
                DateTime dt = DateTime.MinValue;
                DateTime.TryParse(time, out dt);
                return dt;
            }
    
        }
    

     ps:缺少对象在其他文章中复述

  • 相关阅读:
    几种开源工作流引擎的简单比较(转)
    ExecuteScalar
    机房重构---我们“重构”出了什么?
    薏米红豆粥功效及做法介绍
    Mean Shift具体介绍
    linux fork函数浅析
    html的下拉框的几个基本使用方法
    Readprocessmemory使用方法
    配置Log4j(非常具体)
    【Linux】linux经常使用基本命令
  • 原文地址:https://www.cnblogs.com/bridgew/p/12709072.html
Copyright © 2011-2022 走看看