113 lines
2.7 KiB
C#
113 lines
2.7 KiB
C#
using System;
|
|
|
|
namespace Pilz.Configuration
|
|
{
|
|
public class AutoSaveConfigurationManager : ConfigurationManager
|
|
{
|
|
private bool addedHandler = false;
|
|
private bool enableAutoSave = false;
|
|
private string _ConfigFilePath = string.Empty;
|
|
private bool _AutoLoadConfigOnAccess = false;
|
|
|
|
public string ConfigFilePath
|
|
{
|
|
get
|
|
{
|
|
return _ConfigFilePath;
|
|
}
|
|
|
|
set
|
|
{
|
|
_ConfigFilePath = value;
|
|
if (AutoLoadConfigOnAccess)
|
|
Load();
|
|
}
|
|
}
|
|
|
|
public bool AutoLoadConfigOnAccess
|
|
{
|
|
get
|
|
{
|
|
return _AutoLoadConfigOnAccess;
|
|
}
|
|
|
|
set
|
|
{
|
|
_AutoLoadConfigOnAccess = value;
|
|
if (value)
|
|
Load();
|
|
}
|
|
}
|
|
|
|
public bool AutoSaveConfigOnExit
|
|
{
|
|
get
|
|
{
|
|
return enableAutoSave;
|
|
}
|
|
|
|
set
|
|
{
|
|
if (enableAutoSave != value)
|
|
{
|
|
enableAutoSave = value;
|
|
switch (enableAutoSave)
|
|
{
|
|
case true:
|
|
{
|
|
AddAutoSaveHandler();
|
|
break;
|
|
}
|
|
|
|
case false:
|
|
{
|
|
RemoveAutoSaveHandler();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void AddAutoSaveHandler()
|
|
{
|
|
if (!addedHandler)
|
|
{
|
|
System.Windows.Forms.Application.ApplicationExit += AutoSaveSettingsOnExit;
|
|
addedHandler = true;
|
|
}
|
|
}
|
|
|
|
private void RemoveAutoSaveHandler()
|
|
{
|
|
System.Windows.Forms.Application.ApplicationExit -= AutoSaveSettingsOnExit;
|
|
addedHandler = false;
|
|
}
|
|
|
|
private void AutoSaveSettingsOnExit(object sender, EventArgs e)
|
|
{
|
|
Save();
|
|
}
|
|
|
|
private void Save()
|
|
{
|
|
if (!string.IsNullOrEmpty(ConfigFilePath) && Configuration is object)
|
|
{
|
|
Configuration.WriteToFile(ConfigFilePath);
|
|
}
|
|
}
|
|
|
|
private void Load()
|
|
{
|
|
if (!string.IsNullOrEmpty(ConfigFilePath))
|
|
{
|
|
Configuration.ReadFromFile(ConfigFilePath);
|
|
}
|
|
}
|
|
|
|
~AutoSaveConfigurationManager()
|
|
{
|
|
RemoveAutoSaveHandler();
|
|
}
|
|
}
|
|
} |