20 using System.Collections.Generic;
24 using YamlDotNet.Core;
25 using YamlDotNet.Serialization;
58 _lazy =
new Lazy<UserSettingsBase>(() => value);
62 private static Lazy<UserSettingsBase> _lazy =
new Lazy<UserSettingsBase>(() =>
new UserSettingsBase());
80 protected static T FromFileOrDefault<T>(
string yamlFile, DeserializerBuilder deserializerBuilder)
83 if (deserializerBuilder == null)
85 throw new ArgumentNullException(
"deserializerBuilder",
86 "Must provide a DeserializerBuilder object to load settings.");
88 T optionsStore = null;
89 if (File.Exists(yamlFile))
93 using (FileStream fs = File.Open(yamlFile, FileMode.Open, FileAccess.Read, FileShare.Read))
95 StreamReader sr =
new StreamReader(fs);
96 Deserializer des = deserializerBuilder.Build();
97 optionsStore = des.Deserialize<T>(sr);
98 if (optionsStore != null)
100 optionsStore.WasFromFile =
true;
103 Logger.Info(
"Loaded user settings from file '{0}'", yamlFile);
105 catch (IOException e)
107 optionsStore = CreateDefaultOnException<T>(e);
109 catch (YamlException e)
111 optionsStore = CreateDefaultOnException<T>(e);
114 if (optionsStore == null)
116 Logger.Info(
"Creating user settings object from scratch");
117 optionsStore =
new T();
133 protected static T FromFileOrDefault<T>(
string yamlFile)
134 where T : UserSettingsBase,
new()
136 return FromFileOrDefault<T>(yamlFile,
new DeserializerBuilder().IgnoreUnmatchedProperties());
141 #region User settings 175 public bool CcUserOnExceptionReport
179 if (_ccUserOnExceptionReport == null)
181 _ccUserOnExceptionReport =
true;
183 return (
bool)_ccUserOnExceptionReport;
187 _ccUserOnExceptionReport = value;
191 public string DownloadFolder
195 if (_downloadFolder == null)
197 _downloadFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
199 return _downloadFolder;
203 _downloadFolder = value;
209 #region Public static methods 223 _lazy =
new Lazy<UserSettingsBase>(() =>
new UserSettingsBase());
228 #region Properties that are not being saved to the YAML file 235 public bool WasFromFile {
get;
protected set; }
241 public Exception Exception {
get;
protected set; }
247 public UserSettingsBase() { }
260 string fn = GetSettingsFilePath();
261 Directory.CreateDirectory(
System.IO.Path.GetDirectoryName(fn));
262 using (FileStream fs = File.Open(fn, FileMode.Create, FileAccess.Write))
264 StreamWriter sw =
new StreamWriter(fs, Encoding.UTF8);
266 Serializer ser = ConstructSerializerBuilder().Build();
267 ser.Serialize(sw,
this);
271 Logger.Info(
"Saved user settings to file '{0}'", fn);
273 catch (IOException e)
275 Logger.Warn(
"Could not save user settings", e);
294 return new SerializerBuilder();
305 return System.IO.Path.Combine(
306 Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
307 Properties.Settings.Default.UserSettingsPath,
308 Properties.Settings.Default.UserSettingsFile);
317 streamWriter.WriteLine(
"# !!! DO NOT EDIT THIS FILE WHILE THE APPLICATION IS RUNNING !!!");
322 #region Private fields 324 private string _user;
325 private string _email;
331 private bool? _ccUserOnExceptionReport;
333 private string _downloadFolder;
337 #region Private static method 346 private static T CreateDefaultOnException<T>(Exception e)
347 where T: UserSettingsBase,
new()
349 Logger.Warn(e,
"Exception during loading of user settings, using default");
351 options.Exception = e;
359 private static NLog.Logger Logger {
get {
return _logger.Value; } }
361 private static readonly Lazy<
NLog.Logger> _logger =
new Lazy<
NLog.Logger>(() =>
NLog.LogManager.GetCurrentClassLogger());
static void LoadDefaults()
Creates a new settings object without loading the saved settings from file and without saving the cur...
void Save()
Saves the user settings to a file.
Base class for persistent settings; a replacement for the UserSettings.UserSettingsBase system which ...
virtual string GetSettingsFilePath()
Gets the complete path and file name for the user settings file.
virtual SerializerBuilder ConstructSerializerBuilder()
Builds a YamlDotNet serializer builder.
virtual void WriteYamlHeader(StreamWriter streamWriter)
Writes a header to the YAML file before all other data.