示例#1
0
 public bool Save(CfgData data)
 {
     try
     {
         using var writer = File.CreateText(_fileName);
         foreach (var row in data)
         {
             writer.WriteLine(row.Original);
         }
         writer.Flush();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
示例#2
0
        public static CfgData ToCfgData(this IEnumerable <ISettingControl> settingControls, bool withComments = false)
        {
            var cfgData = new CfgData();

            foreach (var control in settingControls)
            {
                if (control.HasValue)
                {
                    if (withComments)
                    {
                        cfgData.AddCommentRow(control.Model.Name);
                    }
                    cfgData.AddOrUpdateRow(control.Model.Key, control.Value);
                }
            }
            return(cfgData);
        }
示例#3
0
        public async Task <bool> SaveAsync(CfgData data)
        {
            try
            {
                using var writer = File.CreateText(_fileName);
                foreach (var row in data)
                {
                    await writer.WriteLineAsync(row.Original);
                }
                await writer.FlushAsync();

                return(true);
            }
            catch
            {
                return(false);
            }
        }
示例#4
0
        public CfgData Read()
        {
            var data = new CfgData(this);

            if (!File.Exists(_fileName))
            {
                return(data);
            }

            try
            {
                using var reader = File.OpenText(_fileName);
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    data.AddRow(line);
                }
            }
            catch { }

            return(data);
        }
示例#5
0
        public static ISet <string> GetUnsupportedSettings(this IEnumerable <ISettingControl> settingControls, CfgData cfgData)
        {
            var unsupportedSettings = new HashSet <string>(cfgData.ToDictionary().Keys, StringComparer.OrdinalIgnoreCase);

            if (unsupportedSettings.Count != 0)
            {
                foreach (var control in settingControls)
                {
                    if (unsupportedSettings.Remove(control.Model.Key) && unsupportedSettings.Count == 0)
                    {
                        break;
                    }
                }
            }
            return(unsupportedSettings);
        }
示例#6
0
        public static List <string> LoadFrom(this IEnumerable <ISettingControl> settingControls, CfgData cfgData)
        {
            var invalidSettings = new List <string>();

            foreach (var control in settingControls)
            {
                if (cfgData.TryGetValue(control.Model.Key, out var value) && value != null)
                {
                    try
                    {
                        control.Value = value;
                    }
                    catch (Exception e)
                    {
                        _logger.Warn(e, $"Invalid setting value {control.Model.Key}={value}. Reset to default");
                        // if value corrupted reset it to default
                        control.ClearValue();
                        invalidSettings.Add(control.Model.Key);
                    }
                }
                else
                {
                    control.ClearValue();
                }
            }
            return(invalidSettings);
        }