mirror of
https://github.com/ckaczor/Common.git
synced 2026-01-13 17:22:40 -05:00
Add backup/restore of settings
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
using System.Configuration;
|
using Common.Debug;
|
||||||
|
using System;
|
||||||
|
using System.Configuration;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Reflection;
|
||||||
using Common.Debug;
|
|
||||||
|
|
||||||
namespace Common.Settings
|
namespace Common.Settings
|
||||||
{
|
{
|
||||||
@@ -16,11 +17,11 @@ namespace Common.Settings
|
|||||||
FileInfo configFile = new FileInfo(config.FilePath);
|
FileInfo configFile = new FileInfo(config.FilePath);
|
||||||
|
|
||||||
// If we can't get the parent directories then don't bother
|
// If we can't get the parent directories then don't bother
|
||||||
if (configFile.Directory == null || configFile.Directory.Parent == null)
|
if (configFile.Directory?.Parent == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Loop over all directories of the configuration file parent directory
|
// Loop over all directories of the configuration file parent directory
|
||||||
foreach (DirectoryInfo directory in configFile.Directory.Parent.GetDirectories())
|
foreach (var directory in configFile.Directory.Parent.GetDirectories())
|
||||||
{
|
{
|
||||||
// Delete the directory if it isn't the current config file directory
|
// Delete the directory if it isn't the current config file directory
|
||||||
if (directory.FullName != configFile.Directory.FullName)
|
if (directory.FullName != configFile.Directory.FullName)
|
||||||
@@ -37,5 +38,53 @@ namespace Common.Settings
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void BackupSettings()
|
||||||
|
{
|
||||||
|
var settingsFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
|
||||||
|
var destination = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\..\last.config";
|
||||||
|
File.Copy(settingsFile, destination, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void RestoreSettings()
|
||||||
|
{
|
||||||
|
var destFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
|
||||||
|
var sourceFile = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\..\last.config";
|
||||||
|
|
||||||
|
if (!File.Exists(sourceFile))
|
||||||
|
return;
|
||||||
|
|
||||||
|
var destDirectory = Path.GetDirectoryName(destFile);
|
||||||
|
|
||||||
|
if (destDirectory == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(destDirectory);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
Console.WriteLine(exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
File.Copy(sourceFile, destFile, true);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
Console.WriteLine(exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
File.Delete(sourceFile);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
Console.WriteLine(exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user