ConfigRW.cs 4.4 KB
using System;
using System.Collections.Generic;

namespace DAL
{
    public class ConfigRW
    {
        private readonly Dictionary<string, string> config = new();
        private readonly System.Xml.XmlDocument doc = new();
        private readonly System.Xml.XmlNode root;
        private readonly string filePath;
        private bool changed = false;

        public ConfigRW(string path)
        {
            filePath = path;
            Model.LogNet.log.Info($"读取配置文件");
            Model.LogNet.log.Debug($"配置文件目录:{path}");

            try
            {
                doc.Load(filePath);
                root = doc.SelectSingleNode("appSettings");
                for (int i = 0; i < root.ChildNodes.Count; i++)
                {
                    string key = root.ChildNodes[i].Name;
                    string value = root.ChildNodes[i].InnerText;
                    config.Add(key, value);
                }
            }
            catch (Exception ex)
            {
                Model.LogNet.log.Error("ConfigRW", ex);
            }
        }

        public void Read<T>(string key, out T value)
        {
            value = (T)TypeDefault(typeof(T));
            bool rtn = config.TryGetValue(key, out string str);
            if (rtn)
            {
                if (TypeConv(typeof(T), str, out object obj))
                    value = (T)obj;
            }
            //Model.LogNet.log.Debug($"Read Config {key}:{value}");
        }

        public T Read<T>(string key)
        {
            Read(key, out T value);
            return value;
        }

        public T Read<T>(string key, T defaultValue)
        {
            T value = defaultValue;
            bool rtn = config.TryGetValue(key, out string str);
            if (rtn)
            {
                if (TypeConv(typeof(T), str, out object obj))
                    value = (T)obj;
            }
            //Model.LogNet.log.Debug($"Read Config {key}:{value}");
            return value;
        }

        public void Write<T>(string key, T value)
        {
            if (config.ContainsKey(key))
            {
                config[key] = value.ToString();
                root.SelectSingleNode(key).InnerText = config[key];
            }
            else
            {
                config.Add(key, value.ToString());
                System.Xml.XmlElement node = doc.CreateElement(key);
                node.InnerText = config[key];
                root.AppendChild(node);
            }
            changed = true;
            Model.LogNet.log.Debug($"Write Config {key}:{value}");
        }

        public void Save()
        {
            if (changed)
            {
                doc.Save(filePath);
                changed = false;
                Model.LogNet.log.Info("Save Config");
            }
        }




        private bool TypeConv(Type type, string str, out object obj)
        {
            bool rtn = false;
            obj = null;

            if (type == typeof(string))
            {
                obj = str;
                rtn = true;
            }
            else if (type == typeof(int))
            {
                if (int.TryParse(str, out int result))
                {
                    obj = result;
                    rtn = true;
                }
            }
            else if (type == typeof(float))
            {
                if (float.TryParse(str, out float result))
                {
                    obj = result;
                    rtn = true;
                }
            }
            else if (type == typeof(double))
            {
                if (double.TryParse(str, out double result))
                {
                    obj = result;
                    rtn = true;
                }
            }
            else if (type == typeof(bool))
            {
                if (bool.TryParse(str, out bool result))
                {
                    obj = result;
                    rtn = true;
                }
            }

            return rtn;
        }

        private object TypeDefault(Type type)
        {
            if (type == typeof(string))
                return "";
            else if (type == typeof(int) || type == typeof(float) || type == typeof(double))
                return 0;
            else if (type == typeof(bool))
                return false;
            else
                return null;
        }

    }
}