仿NewLife的XmlConfig类实现Json配置文件

学习NewLife的配置类XmlConfig实现的边界配置文件读写类

UserProfile

csharp 复制代码
using System;
using System.IO;
using System.Reflection;

namespace HeBingSQL
{
    /* 使用示例
    [ConfigFile(".hebingsql")]
    public class AppConfig : UserProfile<AppConfig >
    {
        public bool AutoStart { get; set; } = true;
    }
    */
    /// <summary>
    /// 把配置文件保存到用户目录下的通用配置基类
    /// </summary>
    /// <typeparam name="T">业务配置类</typeparam>
    public class UserProfile<T> where T : new()
    {
        public static T _current;

        private static string ConfigFilePath
        {
            get
            {
                var attr = typeof(T).GetCustomAttribute<ConfigFileAttribute>();
                var fileName = attr?.FileName ?? ".defaultuncleconfig";
                return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), fileName);
            }
        }

        protected UserProfile()
        {
        }

        public static T Current
        {
            get
            {
                if (_current == null)
                {
                    _current = Load();
                }
                return _current;
            }
        }

        public static T Load()
        {
            // 获取系统用户目录并设置配置文件路径
            string config = "";
            if (File.Exists(ConfigFilePath))
            {
                // 从配置文件中读取快捷方式
                config = File.ReadAllText(ConfigFilePath);
                return System.Text.Json.JsonSerializer.Deserialize<T>(config);
            }
            else
            {
                // 创建默认配置文件
                var obj = new T();
                string jsonString = System.Text.Json.JsonSerializer.Serialize(obj);
                File.WriteAllText(ConfigFilePath, jsonString);

                return obj;
            }
        }
        public void Save()
        {
            // 保存配置到文件的逻辑
            // 这里可以使用序列化库将当前对象保存为XML或JSON格式
            string jsonString = System.Text.Json.JsonSerializer.Serialize(_current);
            File.WriteAllText(ConfigFilePath, jsonString);
        }
    }

	// 读取配置文件名的特性类
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public class ConfigFileAttribute : Attribute
    {
        public string FileName { get; private set; }

        public ConfigFileAttribute(string fileName)
        {
            FileName = fileName;
        }
    }
}

使用

1,新增一个APP的配置类

csharp 复制代码
[ConfigFile(".hebingsql")]
public class AppConfig : UserProfile<AppConfig>
{
    public bool AutoStart { get; set; } = true;
}

2,使用

csharp 复制代码
var tmp = MyConfig.Current.AutoStart; // 读取
MyConfig.Current.AutoStart = false; // 赋值
MyConfig.Current.Save(); // 保存
相关推荐
大飞pkz5 小时前
【设计模式】解释器模式
开发语言·设计模式·c#·解释器模式
敲敲敲-敲代码8 小时前
web系统(asp.net和C#)
前端·c#·asp.net
__XYZ8 小时前
Vala编程语言高级特性-弱引用和所有权
c语言·开发语言·后端·c#
大飞pkz13 小时前
【设计模式】责任链模式
开发语言·设计模式·c#·责任链模式
大飞pkz15 小时前
【设计模式】六大基本原则
开发语言·设计模式·c#·六大原则
椒颜皮皮虾17 小时前
DeploySharp开源发布:让C#部署深度学习模型更加简单
c#·openvino
Rotion_深21 小时前
海康 智能相机二开 绘制底图+测试工具应用框
c#·二次开发·海康·智能相机
王家视频教程图书馆1 天前
C# asp.net模板代码简单API请求
开发语言·c#·asp.net
大飞pkz1 天前
【设计模式】备忘录模式
开发语言·设计模式·c#·备忘录模式