Instantly share code, notes, and snippets.
Last active
August 29, 2015 14:03
Star
You must be signed in to star a gist
A basic class for shared settings, allowing a key/value based settings file to be created or loaded, Lines starting with a # are used as comment lines and are ignored as settings. As an example, the class can be instantiated with: private SharedSettings sharedsettings = new SharedSettings("Contoso", "Test.cfg");
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// The shared settings class, responsible for reading and writing machine-wide shared settings. | |
/// </summary> | |
public class SharedSettings | |
{ | |
/// <summary> | |
/// The file path to the shared settings file, computed using the common application data folder for wider OS support. | |
/// </summary> | |
private string sharedSettingsPath; | |
/// <summary> | |
/// The dictionary collection containing an in-memory copy of the shared settings. | |
/// </summary> | |
private Dictionary<string, string> sharedsettings = new Dictionary<string, string>(); | |
/// <summary> | |
/// Initializes a new instance of the <see cref="SharedSettings" /> class and prepares the settings file chosen for use. | |
/// </summary> | |
/// <param name="organisation">The organization folder to use as the parent for any configuration files.</param> | |
/// <param name="filename">the filename to open/create.</param> | |
public SharedSettings(string organisation, string filename) | |
{ | |
this.sharedSettingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), organisation, filename); | |
this.Load(); | |
} | |
/// <summary> | |
/// Reloads the shared settings from file. | |
/// </summary> | |
public void Reload() | |
{ | |
this.sharedsettings.Clear(); | |
this.Load(); | |
} | |
/// <summary> | |
/// Write out the shared settings dictionary to the shared settings file. | |
/// </summary> | |
public void Save() | |
{ | |
using (StreamWriter file = new StreamWriter(this.sharedSettingsPath)) | |
{ | |
foreach (var entry in this.sharedsettings) | |
{ | |
file.WriteLine("{0},{1}", entry.Key, entry.Value); | |
} | |
} | |
} | |
/// <summary> | |
/// Get the value of an existing key from the shared settings dictionary object and return it as a string. | |
/// </summary> | |
/// <param name="setting">The dictionary key, corresponding to the setting you wish to obtain.</param> | |
/// <returns>The value of the setting.</returns> | |
public string GetSharedSetting(string setting) | |
{ | |
return this.sharedsettings[setting]; | |
} | |
/// <summary> | |
/// Get the value of an existing key from the shared settings dictionary object and return it as a decimal value. | |
/// </summary> | |
/// <param name="setting">The dictionary key, corresponding to the setting you wish to obtain.</param> | |
/// <returns>The value of the setting, converted to decimal.</returns> | |
public decimal GetSharedSettingDecimal(string setting) | |
{ | |
return Convert.ToDecimal(this.sharedsettings[setting]); | |
} | |
/// <summary> | |
/// Get the value of an existing key from the shared settings dictionary object and return it as an integer. | |
/// </summary> | |
/// <param name="setting">The dictionary key, corresponding to the setting you wish to obtain.</param> | |
/// <returns>The value of the setting, converted to an integer.</returns> | |
public decimal GetSharedSettingInt32(string setting) | |
{ | |
return Convert.ToInt32(this.sharedsettings[setting]); | |
} | |
/// <summary> | |
/// Set an existing shared setting in the shared settings dictionary object. | |
/// </summary> | |
/// <param name="setting">The dictionary key, corresponding to the setting you wish to change.</param> | |
/// <param name="value">The value you wish to set on the key.</param> | |
public void SetSharedSetting(string setting, string value) | |
{ | |
this.sharedsettings[setting] = value; | |
} | |
/// <summary> | |
/// Adds a new shared setting and saves it to file. | |
/// </summary> | |
/// <param name="key">Name of the setting to add.</param> | |
/// <param name="value">Value of the setting to add.</param> | |
public void AddSharedSetting(string key, string value) | |
{ | |
this.sharedsettings.Add(key, value); | |
this.Save(); | |
} | |
/// <summary> | |
/// Create the shared settings dictionary file and folder structure. | |
/// This will also set the access permissions on the file to Authenticated Users - Modify, so that all users can modify these settings. | |
/// </summary> | |
private void CreateFile() | |
{ | |
if (!Directory.Exists(Path.GetDirectoryName(this.sharedSettingsPath))) | |
{ | |
Directory.CreateDirectory(Path.GetDirectoryName(this.sharedSettingsPath)); | |
} | |
File.CreateText(this.sharedSettingsPath).Close(); | |
FileSecurity fileaccess = File.GetAccessControl(this.sharedSettingsPath); | |
fileaccess.AddAccessRule(new FileSystemAccessRule("Authenticated Users", FileSystemRights.Modify, AccessControlType.Allow)); | |
File.SetAccessControl(this.sharedSettingsPath, fileaccess); | |
} | |
/// <summary> | |
/// Loads the shared settings from file. | |
/// </summary> | |
private void Load() | |
{ | |
if (!File.Exists(this.sharedSettingsPath)) | |
{ | |
this.CreateFile(); | |
} | |
foreach (string line in File.ReadLines(this.sharedSettingsPath)) | |
{ | |
if (line.StartsWith("#")) | |
{ | |
continue; | |
} | |
string[] setting = line.Split(new char[] { ',' }, 2); | |
this.sharedsettings.Add(setting[0], setting[1]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment