SystemTrayMenu/Config.cs
Markus Hofknecht 828fda03af [BUG] autostart activate not always working #28 https://github.com/Hofknecht/SystemTrayMenu/issues/28
-upgrade earlier before load shortcut
-logging upgrade of settings / config and write config location into log

[BUG] Nullref when shortcut not possible to register #29 https://github.com/Hofknecht/SystemTrayMenu/issues/29
-nullref exception still possible -> do not start contextmenu before shortcut registered
2019-07-08 19:57:15 +02:00

80 lines
2.3 KiB
C#

using Clearcove.Logging;
using Microsoft.WindowsAPICodePack.Dialogs;
using System.Diagnostics;
using System.IO;
using System.Reflection;
namespace SystemTrayMenu
{
public class Config
{
public static string Language = "en";
public static string Path
{
get
{
return Properties.Settings.Default.PathDirectory;
}
}
public static void UpgradeIfNotUpgraded()
{
if (!Properties.Settings.Default.IsUpgraded)
{
// configs located at "%localappdata%\<AssemblyCompany>\"
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.IsUpgraded = true;
Properties.Settings.Default.Save();
FileVersionInfo versionInfo = FileVersionInfo.
GetVersionInfo(Assembly.GetEntryAssembly().Location);
new Logger(nameof(Config)).Info($"Settings upgraded from " +
$"%localappdata%\\{versionInfo.CompanyName}\\");
}
}
public static bool LoadOrSetByUser()
{
bool pathOK = Directory.Exists(
Properties.Settings.Default.PathDirectory);
if (!pathOK)
{
pathOK = SetFolderByUser();
}
return pathOK;
}
public static bool SetFolderByUser()
{
bool pathOK = false;
bool userAborted = false;
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
dialog.InitialDirectory = Path;
dialog.IsFolderPicker = true;
do
{
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
if (Directory.Exists(dialog.FileName))
{
pathOK = true;
Properties.Settings.Default.PathDirectory =
dialog.FileName;
Properties.Settings.Default.Save();
}
}
else
{
userAborted = true;
}
}
while (!pathOK && !userAborted);
return pathOK;
}
}
}