SystemConfig.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace LYFZ.WanYuKeFuData
  6. {
  7. public class SystemConfig
  8. {
  9. public SystemConfig() {
  10. }
  11. /// <summary>
  12. /// 写入配置文件
  13. /// </summary>
  14. /// <param name="model"></param>
  15. /// <param name="configPath"></param>
  16. /// <returns></returns>
  17. public static bool WriteConfig<T>(T model,string configPath)
  18. {
  19. bool writeStatus = true;
  20. try
  21. {
  22. string configInfo = Newtonsoft.Json.JsonConvert.SerializeObject(model);
  23. string tempSaveDirPath = System.IO.Path.GetDirectoryName(configPath);
  24. if (!System.IO.Directory.Exists(tempSaveDirPath))
  25. {
  26. System.IO.Directory.CreateDirectory(tempSaveDirPath);
  27. }
  28. System.IO.File.WriteAllBytes(configPath, Encoding.UTF8.GetBytes(configInfo));
  29. }
  30. catch
  31. {
  32. writeStatus = false;
  33. }
  34. return writeStatus;
  35. }
  36. /// <summary>
  37. /// 读取配置文件
  38. /// </summary>
  39. /// <returns></returns>
  40. public static T ReadConfig<T>(string configPath)
  41. {
  42. T t = Activator.CreateInstance<T>();
  43. if (System.IO.File.Exists(configPath))
  44. {
  45. string configInfo = System.IO.File.ReadAllText(configPath, Encoding.UTF8);
  46. t = (T)Newtonsoft.Json.JsonConvert.DeserializeObject(configInfo, t.GetType());
  47. }
  48. return t;
  49. }
  50. #region 数据库连接配置
  51. static string dataBaseConfigPath = LYFZ.WinAPI.CustomPublicMethod.GetFullDirectoryPath(LYFZ.WinAPI.CustomPublicMethod.BasePath) + "Config\\DBConfig.json";
  52. /// <summary>
  53. /// 服务器数据库配置文件路径
  54. /// </summary>
  55. public static string DataBaseConfigPath
  56. {
  57. get { return SystemConfig.dataBaseConfigPath; }
  58. }
  59. /// <summary>
  60. /// 写入数据库连接配置文件
  61. /// </summary>
  62. /// <param name="dbConn"></param>
  63. /// <returns></returns>
  64. public static bool WriteDBConnectionConfig(LYFZ.WanYuKeFuData.Model.DBConnection dbConn)
  65. {
  66. bool writeStatus = true;
  67. try
  68. {
  69. writeStatus = WriteConfig<LYFZ.WanYuKeFuData.Model.DBConnection>(dbConn,dataBaseConfigPath);
  70. }
  71. catch
  72. {
  73. writeStatus = false;
  74. }
  75. return writeStatus;
  76. }
  77. /// <summary>
  78. /// 读取数据库配置文件
  79. /// </summary>
  80. /// <returns></returns>
  81. public static LYFZ.WanYuKeFuData.Model.DBConnection ReadDBConnectionConfig()
  82. {
  83. return ReadConfig<LYFZ.WanYuKeFuData.Model.DBConnection>(dataBaseConfigPath);
  84. }
  85. #endregion
  86. #region 自动备份工具配置
  87. static string _AutoBackupToolConfigPath = LYFZ.WinAPI.CustomPublicMethod.GetFullDirectoryPath(LYFZ.WinAPI.CustomPublicMethod.BasePath) + "Config\\AutoBackupToolConfig.json";
  88. /// <summary>
  89. /// 配置文件路径
  90. /// </summary>
  91. public static string AutoBackupToolConfigPath
  92. {
  93. get { return SystemConfig._AutoBackupToolConfigPath; }
  94. }
  95. /// <summary>
  96. /// 写入配置
  97. /// </summary>
  98. /// <param name="dbConn"></param>
  99. /// <returns></returns>
  100. public static bool WriteAutoBackupToolConfig(LYFZ.WanYuKeFuData.Model.AutoBackupToolConfig config)
  101. {
  102. bool writeStatus = true;
  103. try
  104. {
  105. writeStatus = WriteConfig<LYFZ.WanYuKeFuData.Model.AutoBackupToolConfig>(config, _AutoBackupToolConfigPath);
  106. }
  107. catch
  108. {
  109. writeStatus = false;
  110. }
  111. return writeStatus;
  112. }
  113. /// <summary>
  114. /// 读取配置
  115. /// </summary>
  116. /// <returns></returns>
  117. public static LYFZ.WanYuKeFuData.Model.AutoBackupToolConfig ReadAutoBackupToolConfig()
  118. {
  119. return ReadConfig<LYFZ.WanYuKeFuData.Model.AutoBackupToolConfig>(_AutoBackupToolConfigPath);
  120. }
  121. #endregion
  122. }
  123. }