using Microsoft.Win32;
using System.Collections.Generic;
namespace FSI.Lib.WinSettings
{
///
/// Specifies the location within the registry where stores
/// settings.
///
public enum RegistrySettingsType
{
///
/// Stores settings in the Windows registry base key HKEY_CURRENT_USER, normally
/// used for storing information about the current user preferences.
///
CurrentUser,
///
/// Stores settings in thee Windows registry base key HKEY_LOCAL_MACHINE, normally
/// use for storing configuration data for the local machine.
///
LocalMachine
}
///
/// The class makes it very easy to save your application
/// settings to the system registry.
///
///
///
/// To use the class, simply derive your own settings class from
/// and add the public properties that you want to be
/// saved as settings. You can then call the and
/// methods to read or write those settings to the
/// system registry.
///
///
/// Your derived class' constructor should initialize your settings properties to
/// their default values.
///
///
/// Two attributes are available for public properties in your derived class. The
/// first is . Use this attribute if you
/// want the setting to be encrypted when saved to file. When using this attribute on
/// any property, you must provide a valid encryption password to the
/// constructor.
///
///
/// The second is the . Use this attribute
/// on any properties that are used internally by your code and should not saved to
/// the registry.
///
///
/// All public properties without the
/// attribute must be of one of the supported data types. This includes all the basic
/// data types as well as and .
/// All other types will raise an exception.
///
///
///
/// The following example creates a settings class called MySettings with
/// several properties, two of which are encrypted when saved to file.
///
/// public class MySettings : RegistrySettings
/// {
/// // Define properties to be saved to file
/// public string EmailHost { get; set; }
/// public int EmailPort { get; set; }
///
/// // The following properties will be encrypted
/// [EncryptedSetting]
/// public string UserName { get; set; }
/// [EncryptedSetting]
/// public string Password { get; set; }
///
/// // The following property will not be saved to file
/// // Non-public properties are also not saved to file
/// [ExcludedSetting]
/// public DateTime Created { get; set; }
///
/// public MySettings(string companyName, string applicationName, RegistrySettingsType settingsType)
/// : base(companyName, applicationName, settingsType, "Password123")
/// {
/// // Set initial, default property values
/// EmailHost = string.Empty;
/// EmailPort = 0;
/// UserName = string.Empty;
/// Password = string.Empty;
///
/// Created = DateTime.Now;
/// }
/// }
///
///
///
///
public abstract class RegistrySettings : Settings
{
private readonly string SubKeyPath;
private readonly RegistryKey RegistryKey;
///
/// Constructs a new RegistrySettings instance.
///
/// Company name entry in registry.
/// Application name entry in registration.
/// Section to store entries in registry.
/// Encryption password. May be null if no settings
/// use the attribute.
public RegistrySettings(string companyName, string applicationName, RegistrySettingsType settingsType, string password = null)
: base(password)
{
SubKeyPath = string.Format("Software\\{0}\\{1}", companyName, applicationName);
RegistryKey = settingsType == RegistrySettingsType.CurrentUser ? Registry.CurrentUser : Registry.LocalMachine;
}
///
/// Performs internal save operations.
///
/// Settings to be saved.
public override void OnSaveSettings(IEnumerable settings)
{
using (RegistryKey registryKey = RegistryKey.CreateSubKey(SubKeyPath, RegistryKeyPermissionCheck.ReadWriteSubTree))
{
foreach (var setting in settings)
{
var value = setting.GetValue();
if (value != null)
registryKey.SetValue(setting.Name, value);
}
}
}
///
/// Performs internal load operations.
///
/// Settings to be loaded.
public override void OnLoadSettings(IEnumerable settings)
{
using (RegistryKey registryKey = RegistryKey.OpenSubKey(SubKeyPath))
{
if (registryKey != null)
{
foreach (var setting in settings)
{
object value = registryKey.GetValue(setting.Name);
if (value != null)
setting.SetValue(value);
}
}
}
}
}
}