Skip to content

Commit

Permalink
[Feature] USB - Portable user.config and relative folder (#158), vers…
Browse files Browse the repository at this point in the history
…ion 1.0.17.37
  • Loading branch information
Hofknecht committed May 9, 2021
1 parent 94bc94b commit b7038b5
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 87 deletions.
28 changes: 4 additions & 24 deletions Config/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace SystemTrayMenu
{
using System;
using System.Configuration;
using System.Diagnostics;
using System.Drawing;
using System.IO;
Expand Down Expand Up @@ -174,35 +175,14 @@ private static bool IsRegistryValueThisValue(string keyName, string valueName, s

private static void UpgradeIfNotUpgraded()
{
var path = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming).FilePath;
path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
if (!Settings.Default.IsUpgraded)
{
Settings.Default.Upgrade();
Settings.Default.IsUpgraded = true;
Settings.Default.Save();

FileVersionInfo versionInfo = FileVersionInfo.
GetVersionInfo(Assembly.GetEntryAssembly().Location);
string upgradedFromPath = $"%localappdata%\\{versionInfo.CompanyName}\\";
try
{
upgradedFromPath = System.IO.Path.GetFullPath(upgradedFromPath);
}
catch (Exception ex)
{
if (ex is ArgumentException ||
ex is System.Security.SecurityException ||
ex is NotSupportedException ||
ex is PathTooLongException)
{
Log.Warn($"Resolve path {upgradedFromPath} failed", ex);
}
else
{
throw;
}
}

Log.Info($"Settings upgraded from {upgradedFromPath}");
Log.Info($"Settings upgraded from {CustomSettingsProvider.UserConfigPath}");
}
}

Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.17.36")]
[assembly: AssemblyFileVersion("1.0.17.36")]
[assembly: AssemblyVersion("1.0.17.37")]
[assembly: AssemblyFileVersion("1.0.17.37")]
105 changes: 86 additions & 19 deletions Properties/CustomSettingsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ namespace SystemTrayMenu.Properties
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using SystemTrayMenu.Utilities;

internal class CustomSettingsProvider : SettingsProvider
{
Expand Down Expand Up @@ -48,17 +50,39 @@ public override string ApplicationName
/// Gets the setting key this is returning must set before the settings are used.
/// e.g. <c>Properties.Settings.Default.SettingsKey = @"C:\temp\user.config";</c>.
/// </summary>
private static string UserConfigPath => Path.Combine(
public static string UserConfigPath => Path.Combine(
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
$"SystemTrayMenu"),
$"user-{Environment.MachineName}.config");

private static string ConfigPathAssembly => Path.Combine(
Directory.GetParent(Assembly.GetEntryAssembly().Location).FullName,
$"user.config");

/// <summary>
/// Gets or sets in memory storage of the settings values.
/// </summary>
private Dictionary<string, SettingStruct> SettingsDictionary { get; set; }

public static bool IsActivatedConfigPathAssembly()
{
return IsConfigPathAssembly();
}

public static void ActivateConfigPathAssembly()
{
CreateEmptyConfigIfNotExists(ConfigPathAssembly);
}

public static void DeactivateConfigPathAssembly()
{
if (IsConfigPathAssembly())
{
File.Delete(ConfigPathAssembly);
}
}

/// <summary>
/// Override.
/// </summary>
Expand Down Expand Up @@ -151,33 +175,63 @@ public override void SetPropertyValues(SettingsContext context, SettingsProperty
/// Creates an empty user.config file...looks like the one MS creates.
/// This could be overkill a simple key/value pairing would probably do.
/// </summary>
private static void CreateEmptyConfig()
private static void CreateEmptyConfigIfNotExists(string path)
{
XDocument doc = new XDocument();
XDeclaration declaration = new XDeclaration("1.0", "utf-8", "true");
XElement config = new XElement(Config);
XElement userSettings = new XElement(UserSettings);
XElement group = new XElement(typeof(Properties.Settings).FullName);
userSettings.Add(group);
config.Add(userSettings);
doc.Add(config);
doc.Declaration = declaration;
doc.Save(UserConfigPath);
if (!File.Exists(path))
{
// if the config file is not where it's supposed to be create a new one.
XDocument doc = new XDocument();
XDeclaration declaration = new XDeclaration("1.0", "utf-8", "true");
XElement config = new XElement(Config);
XElement userSettings = new XElement(UserSettings);
XElement group = new XElement(typeof(Properties.Settings).FullName);
userSettings.Add(group);
config.Add(userSettings);
doc.Add(config);
doc.Declaration = declaration;
try
{
doc.Save(path);
}
catch (Exception ex)
{
Log.Warn($"Failed to store config at assembly location {path}", ex);
}
}
}

private static bool IsConfigPathAssembly()
{
bool isconfigPathAssembly = false;
try
{
isconfigPathAssembly = File.Exists(ConfigPathAssembly);
}
catch (Exception ex)
{
Log.Warn("IsConfigPathAssembly failed", ex);
}

return isconfigPathAssembly;
}

/// <summary>
/// Loads the values of the file into memory.
/// </summary>
private void LoadValuesFromFile()
{
if (!File.Exists(UserConfigPath))
{
// if the config file is not where it's supposed to be create a new one.
CreateEmptyConfig();
}
CreateEmptyConfigIfNotExists(UserConfigPath);

// load the xml
XDocument configXml = XDocument.Load(UserConfigPath);
XDocument configXml;
if (IsConfigPathAssembly())
{
configXml = XDocument.Load(ConfigPathAssembly);
}
else
{
configXml = XDocument.Load(UserConfigPath);
}

// get all of the <setting name="..." serializeAs="..."> elements.
IEnumerable<XElement> settingElements = configXml.Element(Config).Element(UserSettings).Element(typeof(Properties.Settings).FullName).Elements(Setting);
Expand All @@ -202,7 +256,15 @@ private void LoadValuesFromFile()
private void SaveValuesToFile()
{
// load the current xml from the file.
XDocument import = XDocument.Load(UserConfigPath);
XDocument import;
if (IsConfigPathAssembly())
{
import = XDocument.Load(ConfigPathAssembly);
}
else
{
import = XDocument.Load(UserConfigPath);
}

// get the settings group (e.g. <Company.Project.Desktop.Settings>)
XElement settingsSection = import.Element(Config).Element(UserSettings).Element(typeof(Properties.Settings).FullName);
Expand All @@ -227,6 +289,11 @@ private void SaveValuesToFile()
}
}

if (IsConfigPathAssembly())
{
import.Save(ConfigPathAssembly);
}

import.Save(UserConfigPath);
}

Expand Down
36 changes: 36 additions & 0 deletions Resources/lang.Designer.cs

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

12 changes: 12 additions & 0 deletions Resources/lang.resx
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,16 @@
<data name="To change a color paste a HTML Color Code or double click into a field." xml:space="preserve">
<value>To change a color paste a HTML Color Code or double click into a field.</value>
</data>
<data name="Change to relative folder" xml:space="preserve">
<value>Change to relative folder</value>
</data>
<data name="Store config at assembly location" xml:space="preserve">
<value>Store config at assembly location</value>
</data>
<data name="USB" xml:space="preserve">
<value>USB</value>
</data>
<data name="Open assembly location" xml:space="preserve">
<value>Open assembly location</value>
</data>
</root>
Loading

0 comments on commit b7038b5

Please sign in to comment.