Skip to content

Commit

Permalink
feat: Store settings in the application folder
Browse files Browse the repository at this point in the history
  • Loading branch information
learn-more committed Jan 8, 2021
1 parent dc43d69 commit ccff825
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/LessMsi.Gui/LessMsi.Gui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<SubType>Form</SubType>
</Compile>
<Compile Include="MainFormPresenter.cs" />
<Compile Include="Model\ApplicationSettings.cs" />
<Compile Include="Model\MsiFileItemView.cs" />
<Compile Include="Model\MsiPropertyInfo.cs" />
<Compile Include="Model\CabContainedFileView.cs" />
Expand Down
73 changes: 73 additions & 0 deletions src/LessMsi.Gui/Model/ApplicationSettings.cs
Original file line number Diff line number Diff line change
@@ -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<string> RecentFiles { get; set; } = new List<string>();


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;
}
}
}
}
10 changes: 6 additions & 4 deletions src/LessMsi.Gui/MruMenuStripManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// Authors:
// Scott Willeke ([email protected])
//
using LessMsi.Gui.Model;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
Expand Down Expand Up @@ -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];
Expand All @@ -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();
}

/// <summary>
Expand Down
15 changes: 2 additions & 13 deletions src/LessMsi.Gui/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions src/LessMsi.Gui/Properties/Settings.settings
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="LessMsi.Gui.Properties" GeneratedClassName="Settings">
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)">
<Profiles />
<Settings>
<Setting Name="RecentFiles" Type="System.Collections.Specialized.StringCollection" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
<Settings />
</SettingsFile>

0 comments on commit ccff825

Please sign in to comment.