using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BellwetherBackend.Utility { public static class Setting { private static readonly string dataDirectory = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Data"); public static readonly string settingJsonDataPath = System.IO.Path.Combine(dataDirectory, "setting.ini"); public static readonly List PageNameList = new List() { "大事記", "產品展示", "社會責任" }; public enum PageType : int { FrontPage, History, Product, CSR, } public static readonly Dictionary PageFolderName = new Dictionary() { { PageType.History, "History" }, { PageType.Product, "Product" }, { PageType.CSR, "CSR" }, }; private static SettingJson _settingJsonFile; public static SettingJson settingJsonFile { get { if (_settingJsonFile != null) return _settingJsonFile; else { AnalyzJsonFile(); return _settingJsonFile; } } set { _settingJsonFile = value; //save Json SaveJson(); } } private static void AnalyzJsonFile() { string jsonResult; if (!File.Exists(settingJsonDataPath)) { _settingJsonFile = new SettingJson() { BackHomeSec = 60, }; return; } else { using (StreamReader sr = new StreamReader(settingJsonDataPath)) { jsonResult = sr.ReadToEnd(); } _settingJsonFile = JsonConvert.DeserializeObject(jsonResult); } } private static void SaveJson() { string jsonString;// = JsonConvert.SerializeObject(_settingJsonFile, Newtonsoft.Json.Formatting.Indented); jsonString = JsonConvert.SerializeObject(_settingJsonFile, Newtonsoft.Json.Formatting.Indented); using (StreamWriter sw = new StreamWriter(settingJsonDataPath, false, Encoding.Unicode)) { sw.Write(jsonString); sw.Close(); } } } public class SettingJson { public int BackHomeSec { get; set; } } public class FrontPageJson { public string SolarEnergyUrl { get; set; } } }