Exemplo n.º 1
0
        public static SettingsIoResult LoadFromDisk <T>(ref T type) where T : BaseSettings
        {
            var result   = new SettingsIoResult();
            var fileName = $"{LookupDirectory}//{type.FileName}.json";

            try {
                using (var fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
                    if (fs.Length == 0)
                    {
                        Logger.Debug($"Create default settings file {type.FileName}");
                        WriteToDisk(fs, type.Default, ref result);
                        return(result);
                    }

                    using (var sr = new StreamReader(fs)) {
                        var data = sr.ReadToEnd();
                        type        = (T)JsonConvert.DeserializeObject(data, type.GetType());
                        result.IsOk = true;
                    }
                }
            } catch (Exception e) {
                result.IsOk         = false;
                result.Error        = e;
                result.ErrorMessage = e.Message;
            }

            return(result);
        }
Exemplo n.º 2
0
 private static void WriteToDisk(FileStream fs, BaseSettings settings, ref SettingsIoResult result)
 {
     try {
         var data = SerializeSettings(settings);
         using (var sw = new StreamWriter(fs)) {
             sw.Write(data);
             result.IsOk = true;
         }
     } catch (Exception e) {
         result.IsOk         = false;
         result.Error        = e;
         result.ErrorMessage = e.Message;
     }
 }
Exemplo n.º 3
0
        public static SettingsIoResult UpdateOrCreate(BaseSettings settings)
        {
            var fileName = $"{LookupDirectory}//{settings.FileName}.json";

            using (var fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
                var result = new SettingsIoResult {
                    IsOk         = false,
                    ErrorMessage = "Unknown"
                };

                WriteToDisk(fs, settings, ref result);
                return(result);
            }
        }