Exemplo n.º 1
0
        public static DemoSetting DemoLoad()
        {
            DemoSetting setting = null;

            if (!Directory.Exists($"{MyWorkPath}\\stf"))
            {
                Directory.CreateDirectory($"{MyWorkPath}\\stf");
            }
            string SettingPath = $"{MyWorkPath}\\stf\\Demo.json";

            try
            {
                if (File.Exists(SettingPath))
                {
                    string json = File.ReadAllText(SettingPath, Encoding.UTF8);
                    setting = JsonConvert.DeserializeObject <DemoSetting>(json);
                }
                else
                {
                    DemoSetting Setting = new DemoSetting()
                    {
                        ConnectionFlag = true
                    };
                    setting = Setting;
                    string output = JsonConvert.SerializeObject(setting, Formatting.Indented, new JsonSerializerSettings());
                    File.WriteAllText(SettingPath, output);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, " Demo資訊設定載入錯誤");
            }
            return(setting);
        }
        public void GetDefaultSettingObjectTest()
        {
            InstallDemoFileSys();

            var defaultObj = new DemoSetting {
                Text = "Test"
            };

            var source = new JsonConfigurationSource <DemoSetting>(@"c:\temp\data.config", () => defaultObj);

            var defaultSettingObject = source.GetDefaultSettingObject() as DemoSetting;
            var settingObject        = source.GetSettingObject() as DemoSetting;

            Assert.Equal("Test", defaultSettingObject?.Text);
            Assert.Equal("DemoDataValue", settingObject?.Text);
        }