forked from activescott/lessmsi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Store settings in the application folder
- Loading branch information
1 parent
dc43d69
commit ccff825
Showing
5 changed files
with
84 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
// Authors: | ||
// Scott Willeke ([email protected]) | ||
// | ||
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(); | ||
} | ||
|
||
/// <summary> | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |