Setting.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace BellwetherBackend.Utility
  9. {
  10. public static class Setting
  11. {
  12. private static readonly string dataDirectory = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Data");
  13. public static readonly string settingJsonDataPath = System.IO.Path.Combine(dataDirectory, "setting.ini");
  14. public static readonly List<string> PageNameList = new List<string>() { "大事記", "產品展示", "社會責任" };
  15. public enum PageType : int
  16. {
  17. FrontPage,
  18. History,
  19. Product,
  20. CSR,
  21. }
  22. public static readonly Dictionary<PageType, string> PageFolderName
  23. = new Dictionary<PageType, string>() {
  24. { PageType.History, "History" },
  25. { PageType.Product, "Product" },
  26. { PageType.CSR, "CSR" },
  27. };
  28. private static SettingJson _settingJsonFile;
  29. public static SettingJson settingJsonFile
  30. {
  31. get
  32. {
  33. if (_settingJsonFile != null)
  34. return _settingJsonFile;
  35. else
  36. {
  37. AnalyzJsonFile();
  38. return _settingJsonFile;
  39. }
  40. }
  41. set
  42. {
  43. _settingJsonFile = value;
  44. //save Json
  45. SaveJson();
  46. }
  47. }
  48. private static void AnalyzJsonFile()
  49. {
  50. string jsonResult;
  51. if (!File.Exists(settingJsonDataPath))
  52. {
  53. _settingJsonFile = new SettingJson()
  54. {
  55. BackHomeSec = 60,
  56. };
  57. return;
  58. }
  59. else
  60. {
  61. using (StreamReader sr = new StreamReader(settingJsonDataPath))
  62. {
  63. jsonResult = sr.ReadToEnd();
  64. }
  65. _settingJsonFile = JsonConvert.DeserializeObject<SettingJson>(jsonResult);
  66. }
  67. }
  68. private static void SaveJson()
  69. {
  70. string jsonString;// = JsonConvert.SerializeObject(_settingJsonFile, Newtonsoft.Json.Formatting.Indented);
  71. jsonString = JsonConvert.SerializeObject(_settingJsonFile, Newtonsoft.Json.Formatting.Indented);
  72. using (StreamWriter sw = new StreamWriter(settingJsonDataPath, false, Encoding.Unicode))
  73. {
  74. sw.Write(jsonString);
  75. sw.Close();
  76. }
  77. }
  78. }
  79. public class SettingJson
  80. {
  81. public int BackHomeSec { get; set; }
  82. }
  83. }