using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LYFZ.WanYuKeFuData { public class SystemConfig { public SystemConfig() { } /// /// 写入配置文件 /// /// /// /// public static bool WriteConfig(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; } /// /// 读取配置文件 /// /// public static T ReadConfig(string configPath) { T t = Activator.CreateInstance(); 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"; /// /// 服务器数据库配置文件路径 /// public static string DataBaseConfigPath { get { return SystemConfig.dataBaseConfigPath; } } /// /// 写入数据库连接配置文件 /// /// /// public static bool WriteDBConnectionConfig(LYFZ.WanYuKeFuData.Model.DBConnection dbConn) { bool writeStatus = true; try { writeStatus = WriteConfig(dbConn,dataBaseConfigPath); } catch { writeStatus = false; } return writeStatus; } /// /// 读取数据库配置文件 /// /// public static LYFZ.WanYuKeFuData.Model.DBConnection ReadDBConnectionConfig() { return ReadConfig(dataBaseConfigPath); } #endregion #region 自动备份工具配置 static string _AutoBackupToolConfigPath = LYFZ.WinAPI.CustomPublicMethod.GetFullDirectoryPath(LYFZ.WinAPI.CustomPublicMethod.BasePath) + "Config\\AutoBackupToolConfig.json"; /// /// 配置文件路径 /// public static string AutoBackupToolConfigPath { get { return SystemConfig._AutoBackupToolConfigPath; } } /// /// 写入配置 /// /// /// public static bool WriteAutoBackupToolConfig(LYFZ.WanYuKeFuData.Model.AutoBackupToolConfig config) { bool writeStatus = true; try { writeStatus = WriteConfig(config, _AutoBackupToolConfigPath); } catch { writeStatus = false; } return writeStatus; } /// /// 读取配置 /// /// public static LYFZ.WanYuKeFuData.Model.AutoBackupToolConfig ReadAutoBackupToolConfig() { return ReadConfig(_AutoBackupToolConfigPath); } #endregion } }