Example #1
0
        internal void Save()
        {
            LastSaveTime = DateTimeOffset.Now;

            try
            {
                var filePath = Path.Combine(
                    FolderService.GetAppDataFolderPath(true),
                    SettingsFileName);

                using (var sw = new StreamWriter(filePath, false, Encoding.UTF8))                 // BOM will be emitted.
                    using (var xw = XmlWriter.Create(sw, new XmlWriterSettings {
                        Indent = true
                    }))
                    {
                        var serializer = new DataContractSerializer(typeof(Settings));
                        serializer.WriteObject(xw, this);
                        xw.Flush();
                    }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to save settings to AppData." + Environment.NewLine
                                + ex);
            }
        }
Example #2
0
        private static void RecordToAppData(string content)
        {
            try
            {
                var filePath = Path.Combine(
                    FolderService.GetAppDataFolderPath(true),
                    LogFileName);

                UpdateText(filePath, content);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to record exception log to AppData." + Environment.NewLine
                                + ex);
            }
        }
Example #3
0
        private static void RecordToAppData(string fileName, string content)
        {
            try
            {
                FolderService.AssureAppDataFolder();

                var appDataFilePath = Path.Combine(
                    FolderService.AppDataFolderPath,
                    fileName);

                UpdateText(appDataFilePath, content);
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Failed to record log to AppData." + Environment.NewLine
                                + ex);
            }
        }
Example #4
0
        internal void Load()
        {
            try
            {
                var filePath = Path.Combine(
                    FolderService.GetAppDataFolderPath(false),
                    SettingsFileName);

                if (!File.Exists(filePath))
                {
                    return;
                }

                using (var sr = new StreamReader(filePath, Encoding.UTF8))
                    using (var xr = XmlReader.Create(sr))
                    {
                        var serializer = new DataContractSerializer(typeof(Settings));
                        var loaded     = (Settings)serializer.ReadObject(xr);

                        typeof(Settings)
                        .GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
                        .Where(x => x.CanWrite)
                        .ToList()
                        .ForEach(x => x.SetValue(this, x.GetValue(loaded)));
                    }
            }
            catch (SerializationException)
            {
                // Ignore broken settings file.
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to load settings from AppData." + Environment.NewLine
                                + ex);
            }
        }