Search Framework:
SettingsManager
Namespace: WealthLab.Core
Parent: Object

SettingsManager manages a collection of string-keyed settings and persists them to a local file. It supports several common data types, optional file encryption, throttled disk writes, and thread-safe access through internal locking.

Constructors
SettingsManager
public SettingsManager(
string fileName,
bool encrypt = false)

Creates a SettingsManager that loads and saves settings using the specified fileName. If the file already exists, its contents are loaded into the SettingsManager. When encrypt is true, each persisted entry is decrypted when loading and encrypted when saving. A typical location for a settings file can be based on WLHost.Instance.DataFolder.



Data Access
Get
public string Get(
string key,
string defaultValue)
public bool Get(
string key,
bool defaultValue)
public int Get(
string key,
int defaultValue)
public double Get(
string key,
double defaultValue)
public DateTime Get(
string key,
DateTime defaultValue)
public WLColor Get(
string key,
WLColor value)

Returns the value associated with key, converted to the requested type. If key does not exist, the supplied default value is returned. Supported types are:

  • string
  • bool
  • int
  • double
  • DateTime
  • WLColor Double values are parsed using invariant culture. DateTime values are persisted internally using their Ticks value. WLColor values are restored using WLColor.Parse.

Set
public void Set(
string key,
string value)
public void Set(
string key,
bool value)
public void Set(
string key,
int value)
public void Set(
string key,
double value)
public void Set(
string key,
DateTime value)
public void Set(
string key,
WLColor value)

Sets the value associated with key. Supported types are:

  • string
  • bool
  • int
  • double
  • DateTime
  • WLColor Calling Set marks the SettingsManager as requiring a save and attempts to persist the settings according to SaveThrottleSeconds. Double values are persisted using invariant culture. DateTime values are stored using their Ticks value. WLColor values are stored using their string representation.


Members
Cleanup
public static void Cleanup()

Forces all SettingsManager instances created during the current application session to save any pending changes. This method is intended for application shutdown so that settings still waiting because of save throttling are written to disk.


ClearAndDelete
public void ClearAndDelete()

Clears all settings currently held by the SettingsManager and deletes its settings file from disk.


ContainsKey
public bool ContainsKey(string key)

Returns true if the SettingsManager contains the specified key. The comparison is case-insensitive. Returns false if key is null, empty, or contains only whitespace.


FileName
public string FileName

Returns the name of the file used to persist the SettingsManager contents. The file name is established by the constructor.


FullDictionary
public Dictionary<string, string> FullDictionary

Returns the underlying Dictionary containing all settings. The Dictionary stores both keys and values as strings.


IsEncrypted
public bool IsEncrypted

Gets or sets whether SettingsManager encrypts entries when saving them to disk. The initial value is established by the encrypt constructor parameter.


Keys
public List<string> Keys

Returns a List containing the keys currently stored by the SettingsManager.


Remove
public void Remove(string key)

Removes the setting associated with key. Null or empty keys are ignored. After removing the entry, the SettingsManager marks its contents for saving and attempts to save according to SaveThrottleSeconds.


SaveSettings
public void SaveSettings(
bool fromSet = false)

Attempts to save the current settings to disk. When fromSet is true, the SettingsManager marks itself as requiring a save. If no save is required, the method returns without writing the file. If a save is required but the interval specified by SaveThrottleSeconds has not elapsed since the previous save, the write is deferred until a later call. When a save occurs, settings are written as sorted key=value lines. If IsEncrypted is true, each line is encrypted before being written.


SaveThrottleSeconds
public int SaveThrottleSeconds

Gets or sets the minimum number of seconds between automatic writes to disk. The default value is 1. Calling Set or Remove marks the SettingsManager as needing to save, but repeated changes made within the throttle interval can be combined into a later disk write rather than causing a write for every change.