123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace LYFZ.WanYuKeFuData
- {
- public class SystemConfig
- {
- public SystemConfig() {
-
- }
- /// <summary>
- /// 写入配置文件
- /// </summary>
- /// <param name="model"></param>
- /// <param name="configPath"></param>
- /// <returns></returns>
- public static bool WriteConfig<T>(T model,string configPath)
- {
- bool writeStatus = true;
- try
- {
- string configInfo = Newtonsoft.Json.JsonConvert.SerializeObject(model);
- string tempSaveDirPath = System.IO.Path.GetDirectoryName(configPath);
- if (!System.IO.Directory.Exists(tempSaveDirPath))
- {
- System.IO.Directory.CreateDirectory(tempSaveDirPath);
- }
- System.IO.File.WriteAllBytes(configPath, Encoding.UTF8.GetBytes(configInfo));
- }
- catch
- {
- writeStatus = false;
- }
- return writeStatus;
- }
- /// <summary>
- /// 读取配置文件
- /// </summary>
- /// <returns></returns>
- public static T ReadConfig<T>(string configPath)
- {
- T t = Activator.CreateInstance<T>();
- if (System.IO.File.Exists(configPath))
- {
- string configInfo = System.IO.File.ReadAllText(configPath, Encoding.UTF8);
- t = (T)Newtonsoft.Json.JsonConvert.DeserializeObject(configInfo, t.GetType());
- }
- return t;
- }
- #region 数据库连接配置
- static string dataBaseConfigPath = LYFZ.WinAPI.CustomPublicMethod.GetFullDirectoryPath(LYFZ.WinAPI.CustomPublicMethod.BasePath) + "Config\\DBConfig.json";
- /// <summary>
- /// 服务器数据库配置文件路径
- /// </summary>
- public static string DataBaseConfigPath
- {
- get { return SystemConfig.dataBaseConfigPath; }
- }
- /// <summary>
- /// 写入数据库连接配置文件
- /// </summary>
- /// <param name="dbConn"></param>
- /// <returns></returns>
- public static bool WriteDBConnectionConfig(LYFZ.WanYuKeFuData.Model.DBConnection dbConn)
- {
- bool writeStatus = true;
- try
- {
- writeStatus = WriteConfig<LYFZ.WanYuKeFuData.Model.DBConnection>(dbConn,dataBaseConfigPath);
- }
- catch
- {
- writeStatus = false;
- }
- return writeStatus;
- }
- /// <summary>
- /// 读取数据库配置文件
- /// </summary>
- /// <returns></returns>
- public static LYFZ.WanYuKeFuData.Model.DBConnection ReadDBConnectionConfig()
- {
- return ReadConfig<LYFZ.WanYuKeFuData.Model.DBConnection>(dataBaseConfigPath);
- }
- #endregion
- #region 自动备份工具配置
- static string _AutoBackupToolConfigPath = LYFZ.WinAPI.CustomPublicMethod.GetFullDirectoryPath(LYFZ.WinAPI.CustomPublicMethod.BasePath) + "Config\\AutoBackupToolConfig.json";
- /// <summary>
- /// 配置文件路径
- /// </summary>
- public static string AutoBackupToolConfigPath
- {
- get { return SystemConfig._AutoBackupToolConfigPath; }
- }
- /// <summary>
- /// 写入配置
- /// </summary>
- /// <param name="dbConn"></param>
- /// <returns></returns>
- public static bool WriteAutoBackupToolConfig(LYFZ.WanYuKeFuData.Model.AutoBackupToolConfig config)
- {
- bool writeStatus = true;
- try
- {
- writeStatus = WriteConfig<LYFZ.WanYuKeFuData.Model.AutoBackupToolConfig>(config, _AutoBackupToolConfigPath);
- }
- catch
- {
- writeStatus = false;
- }
- return writeStatus;
- }
- /// <summary>
- /// 读取配置
- /// </summary>
- /// <returns></returns>
- public static LYFZ.WanYuKeFuData.Model.AutoBackupToolConfig ReadAutoBackupToolConfig()
- {
- return ReadConfig<LYFZ.WanYuKeFuData.Model.AutoBackupToolConfig>(_AutoBackupToolConfigPath);
- }
- #endregion
- }
- }
|