From ccff825ca298e19b214fbc9b57d83560d8e16b53 Mon Sep 17 00:00:00 2001 From: Mark Jansen Date: Fri, 8 Jan 2021 18:51:43 +0100 Subject: [PATCH] feat: Store settings in the application folder #137 --- src/LessMsi.Gui/LessMsi.Gui.csproj | 1 + src/LessMsi.Gui/Model/ApplicationSettings.cs | 73 +++++++++++++++++++ src/LessMsi.Gui/MruMenuStripManager.cs | 10 ++- .../Properties/Settings.Designer.cs | 15 +--- src/LessMsi.Gui/Properties/Settings.settings | 8 +- 5 files changed, 84 insertions(+), 23 deletions(-) create mode 100644 src/LessMsi.Gui/Model/ApplicationSettings.cs diff --git a/src/LessMsi.Gui/LessMsi.Gui.csproj b/src/LessMsi.Gui/LessMsi.Gui.csproj index 25d2784..99a6ec2 100644 --- a/src/LessMsi.Gui/LessMsi.Gui.csproj +++ b/src/LessMsi.Gui/LessMsi.Gui.csproj @@ -73,6 +73,7 @@ Form + diff --git a/src/LessMsi.Gui/Model/ApplicationSettings.cs b/src/LessMsi.Gui/Model/ApplicationSettings.cs new file mode 100644 index 0000000..239d930 --- /dev/null +++ b/src/LessMsi.Gui/Model/ApplicationSettings.cs @@ -0,0 +1,73 @@ +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using System.Xml.Serialization; + +namespace LessMsi.Gui.Model +{ + public class ApplicationSettings + { + static string applicationSettingsFile; + static ApplicationSettings applicationSettings; + + public List RecentFiles { get; set; } = new List(); + + + public void Save() + { + try + { + using (TextWriter writer = new StreamWriter(ApplicationSettingsFile)) + { + XmlSerializer serializer = new XmlSerializer(typeof(ApplicationSettings)); + serializer.Serialize(writer, this); + } + } + catch + { + // Should we nag the user? + } + } + + static string ApplicationSettingsFile + { + get + { + if (applicationSettingsFile == null) + { + string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + applicationSettingsFile = Path.Combine(directory, "lessmsi-gui-settings.xml"); + } + return applicationSettingsFile; + } + } + + public static ApplicationSettings Default + { + get + { + if (applicationSettings == null) + { + try + { + using (FileStream fs = new FileStream(ApplicationSettingsFile, FileMode.Open)) + { + XmlSerializer serializer = new XmlSerializer(typeof(ApplicationSettings)); + applicationSettings = (ApplicationSettings)serializer.Deserialize(fs); + } + } + catch + { + applicationSettings = null; + } + if (applicationSettings == null) + { + applicationSettings = new ApplicationSettings(); + } + } + + return applicationSettings; + } + } + } +} diff --git a/src/LessMsi.Gui/MruMenuStripManager.cs b/src/LessMsi.Gui/MruMenuStripManager.cs index 877d449..2cd7e7a 100644 --- a/src/LessMsi.Gui/MruMenuStripManager.cs +++ b/src/LessMsi.Gui/MruMenuStripManager.cs @@ -22,6 +22,7 @@ // Authors: // Scott Willeke (scott@willeke.com) // +using LessMsi.Gui.Model; using System; using System.Collections.Generic; using System.Collections.Specialized; @@ -66,7 +67,7 @@ public MruMenuStripManager(ToolStripMenuItem placeHolderItem) private void LoadPreferences() { - var recentFiles = Properties.Settings.Default.RecentFiles; + var recentFiles = ApplicationSettings.Default.RecentFiles; if (recentFiles == null) return; var paths = new string[recentFiles.Count]; @@ -85,9 +86,10 @@ private void LoadPreferences() public void SavePreferences() { PruneItems(); - var paths = new StringCollection(); - _items.ForEach((item) => paths.Add(item.FilePathName)); - Properties.Settings.Default.RecentFiles = paths; + //var paths = new StringCollection(); + ApplicationSettings.Default.RecentFiles.Clear(); + _items.ForEach((item) => ApplicationSettings.Default.RecentFiles.Add(item.FilePathName)); + ApplicationSettings.Default.Save(); } /// diff --git a/src/LessMsi.Gui/Properties/Settings.Designer.cs b/src/LessMsi.Gui/Properties/Settings.Designer.cs index ce5c8cf..9c0d2ce 100644 --- a/src/LessMsi.Gui/Properties/Settings.Designer.cs +++ b/src/LessMsi.Gui/Properties/Settings.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.17929 +// Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -12,7 +12,7 @@ namespace LessMsi.Gui.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.8.1.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); @@ -22,16 +22,5 @@ public static Settings Default { return defaultInstance; } } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - public global::System.Collections.Specialized.StringCollection RecentFiles { - get { - return ((global::System.Collections.Specialized.StringCollection)(this["RecentFiles"])); - } - set { - this["RecentFiles"] = value; - } - } } } diff --git a/src/LessMsi.Gui/Properties/Settings.settings b/src/LessMsi.Gui/Properties/Settings.settings index bed8783..8e615f2 100644 --- a/src/LessMsi.Gui/Properties/Settings.settings +++ b/src/LessMsi.Gui/Properties/Settings.settings @@ -1,9 +1,5 @@  - + - - - - - + \ No newline at end of file