.Net Core 實用的配置文件類 方便統一使用 ConfigInfo

在敲代碼過程中,經常會,左邊寫一個某某配置類,右邊一個某某配置類,然後,對它進行抽象統一,寫了一個,感覺比較好用。

  /// <summary>
    /// 配置信息(用來讀取相應配置文件)
    /// </summary>
    public class ConfigInfo<T> where T : class, new()
    {
        /// <summary>
        /// 配置地址
        /// </summary>
        public string FileName { get; set; }
        /// <summary>
        /// 文件名
        /// </summary>
        public string FilePath { get; set; }
        /// <summary>
        /// 當前配置
        /// </summary>
        public T CurrentConfig { get; set; }
        /// <summary>
        /// 讀寫鎖
        /// </summary>
        private ReaderWriterLockSlim rwl = new ReaderWriterLockSlim();
        /// <summary>
        /// 構造函數
        /// </summary>
        public ConfigInfo(string FileName)
        {
            this.FileName = FileName;
            this.FilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FileName);
            CurrentConfig = Read();
        }
        /// <summary>
        /// 讀取配置信息
        /// </summary>
        /// <returns></returns>
        public T Read()
        {
            T t = new T();
            if (File.Exists(FilePath))
            {
                try
                {
                    rwl.EnterReadLock();
                    var Str = File.ReadAllText(FilePath);
                    if (!string.IsNullOrEmpty(Str))
                    {
                        try
                        {
                            t = JsonConvert.DeserializeObject<T>(Str);
                            CurrentConfig = t;
                            return t;
                        }
                        catch (Exception)
                        { }
                    }
                }
                finally
                {
                    rwl.ExitReadLock();
                }
            }
            else
            {
                Save(t);
            }
            return t;
        }
        /// <summary>
        /// 寫配置
        /// </summary>
        /// <param name="t"></param>
        public void Save(T t)
        {
            try
            {
                rwl.EnterWriteLock();
                if (!File.Exists(FilePath))
                {
                    try
                    {
                        Directory.CreateDirectory(new FileInfo(FilePath).DirectoryName);
                        CurrentConfig = t;
                    }
                    catch (Exception)
                    { }
                }
                File.WriteAllText(FilePath, JsonConvert.SerializeObject(t));
            }
            catch (Exception)
            {
            }
            finally
            {
                rwl.ExitWriteLock();
            }
        }
        /// <summary>
        /// 寫配置
        /// </summary>
        /// <param name="t"></param>
        public void Save()
        {
            try
            {
                rwl.EnterWriteLock();
                if (!File.Exists(FilePath))
                {
                    try
                    {
                        Directory.CreateDirectory(new FileInfo(FilePath).DirectoryName);
                    }
                    catch (Exception)
                    { }
                }
                File.WriteAllText(FilePath, JsonConvert.SerializeObject(CurrentConfig));
            }
            catch (Exception)
            {
            }
            finally
            {
                rwl.ExitWriteLock();
            }
        }
    }

一個簡單的客戶端配置類

 /// <summary>
    /// 客戶端配置
    /// </summary>
    public class ClientConfig
    {
        /// <summary>
        /// 服務地址
        /// </summary>
        public string Server { get; set; }
    }

具體的用法:

 ConfigInfo<ClientConfig> configInfo = new ConfigInfo<ClientConfig>("ClientConfig.cfg");
            configInfo.CurrentConfig.Server = "10";
            configInfo.Save();
            configInfo.Read();
            configInfo.CurrentConfig.Server = "100";
            configInfo.Save();

從結果來看,使用還是很方便的,以後類也方便管理了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章