/// <summary> /// Initialises the application settings. /// </summary> public static void InitializeSettings() { if (Settings != null) { return; // Already done } // EXPLANATION: // Here is the main work for the SettingsAdapterFactory class. Its New method is called // to dynamically create an implementation of the settings interface with all the // defined properties and sub-interfaces. The resulting object has a certain runtime // type which is not known at compile time and thus not accessible from the code. The // concrete type is not relevant, though, because it simply implements the specified // interface which can be used by the code. // // The SettingsAdapterFactory uses a SettingsStore object as its backend. This is where // all settings data is actually read from and written to. You can pull out this line // and create any ISettingsStore object you like. The FileSettingsStore takes a path to // the XML settings file on disk. It also implements deferred saving of the file. // Another implementation, RegistrySettingsStore, connects with the Windows registry // instead of a file and does not defer write operations. Settings = SettingsAdapterFactory.New <IAppSettings>( new FileSettingsStore( SettingsHelper.GetAppDataPath(@"Unclassified\SettingsDemo", "SettingsDemo.conf"))); // EXPLANATION: // The settings schema can change over time. Since the file format directly represents // the interface members, when the code is refactored, the settings values are searched // in a different place. In order to convert old names in a new application, we need to // track which version last wrote the settings file and detect when a newer schema is // used in the current program. The following code checks for older versions of settings // and renames old settings keys directly in the settings store (since we don't have // interface members to access them right now). This allows keeping the value of // previous versions, and also deletes the old names so that they won't gather in the // file over the years. Beware that these are mostly unrelated examples to demonstrate // the possible conversions. // Update settings format from old version if (string.IsNullOrEmpty(Settings.LastStartedAppVersion)) { // Changes before the last started version was tracked Settings.SettingsStore.Rename("play-sounds", "sounds.enabled"); } if (string.IsNullOrEmpty(Settings.LastStartedAppVersion) || new Version(Settings.LastStartedAppVersion).CompareTo(new Version("0.3")) < 0) { // Changes made in version 0.3 Settings.SettingsStore.Rename("window.left", "MainWindowState.Left"); Settings.SettingsStore.Rename("window.top", "MainWindowState.Top"); Settings.SettingsStore.Rename("window.width", "MainWindowState.Width"); Settings.SettingsStore.Rename("window.height", "MainWindowState.Height"); Settings.View.MainWindowState.IsMaximized = Settings.SettingsStore.GetInt("window.state") == 2; Settings.SettingsStore.Remove("window.state"); } // Remember the version of the application. // If we need to react on settings changes from previous application versions, here is // the place to check the version currently in the settings, before it's overwritten. Settings.LastStartedAppVersion = "1.0"; }
/// <summary> /// Initialises the application settings. /// </summary> public static void InitializeSettings() { if (Settings != null) { return; // Already done } Settings = SettingsAdapterFactory.New <IAppSettings>( new FileSettingsStore( SettingsHelper.GetAppDataPath(@"Unclassified\CecilExplorer", "CecilExplorer.conf"))); }
public void RunRegistry() { Registry.CurrentUser.OpenSubKey(@"Software\Unclassified", true).DeleteSubKeyTree("SettingsDemo", false); ISettingsStore settingsStore = new RegistrySettingsStore(false, @"Software\Unclassified\SettingsDemo"); IAppSettings settings = SettingsAdapterFactory.New <IAppSettings>(settingsStore); AccessSettings(settingsStore, settings); settings.SettingsStore.Dispose(); }
/// <summary> /// Initialises the application settings. /// </summary> public static void InitializeSettings() { if (Settings != null) { return; // Already done } Settings = SettingsAdapterFactory.New <IAppSettings>( new FileSettingsStore( SettingsHelper.GetAppDataPath(@"Unclassified\FieldLog", "FieldLogViewer.conf"))); // The settings ShowThreadIdColumn and ShowWebRequestIdColumn are mutually exclusive Settings.OnPropertyChanged( s => s.ShowThreadIdColumn, () => { if (Settings.ShowThreadIdColumn) { Settings.ShowWebRequestIdColumn = false; } }, true); Settings.OnPropertyChanged( s => s.ShowWebRequestIdColumn, () => { if (Settings.ShowWebRequestIdColumn) { Settings.ShowThreadIdColumn = false; } }, true); // Update settings format from old version FL.TraceData("LastStartedAppVersion", Settings.LastStartedAppVersion); if (string.IsNullOrEmpty(Settings.LastStartedAppVersion)) { Settings.SettingsStore.Rename("LastAppVersion", "LastStartedAppVersion"); Settings.SettingsStore.Rename("Window.MainLeft", "MainWindowState.Left"); Settings.SettingsStore.Rename("Window.MainTop", "MainWindowState.Top"); Settings.SettingsStore.Rename("Window.MainWidth", "MainWindowState.Width"); Settings.SettingsStore.Rename("Window.MainHeight", "MainWindowState.Height"); Settings.SettingsStore.Rename("Window.MainIsMaximized", "MainWindowState.IsMaximized"); Settings.SettingsStore.Rename("Window.ToolBarInWindowFrame", "ToolBarInWindowFrame"); Settings.SettingsStore.Rename("Window.SettingsLeft", "SettingsWindowState.Left"); Settings.SettingsStore.Rename("Window.SettingsTop", "SettingsWindowState.Top"); Settings.SettingsStore.Rename("Window.SettingsWidth", "SettingsWindowState.Width"); Settings.SettingsStore.Rename("Window.SettingsHeight", "SettingsWindowState.Height"); } // Remember the version of the application. // If we need to react on settings changes from previous application versions, here is // the place to check the version currently in the settings, before it's overwritten. Settings.LastStartedAppVersion = FL.AppVersion; }
public void RunFile() { File.Delete("settings.xml"); File.Delete("settings.xml.bak"); ISettingsStore settingsStore = new FileSettingsStore("settings.xml"); IAppSettings settings = SettingsAdapterFactory.New <IAppSettings>(settingsStore); AccessSettings(settingsStore, settings); settings.SettingsStore.Dispose(); }
/// <summary> /// Initialises a new instance of the DemoPlugin class that implements the demo plugin. /// </summary> /// <param name="settingsStore">The application's settings store to share by this plugin.</param> /// <param name="pluginSettingsPrefix">The settings key prefix for this plugin in the application's settings store.</param> public DemoPlugin(ISettingsStore settingsStore, string pluginSettingsPrefix) { // EXPLANATION: // The prefix is here assigned by the host application, but it could also be defined by // the plugin itself, provided there are no collisions between different plugins or // multiple instances of them, or at least the properties in the same prefix. // Create an implementation of the plugin's settings interface Settings = SettingsAdapterFactory.New <IDemoPluginSettings>(settingsStore, pluginSettingsPrefix); // Set the demo value Settings.CustomValue = 5; }
/// <summary> /// Initialises the application settings. /// </summary> public static void InitializeSettings() { if (Settings != null) { return; // Already done } Settings = SettingsAdapterFactory.New <IAppSettings>( new FileSettingsStore( SettingsHelper.GetAppDataPath(@"Unclassified\MiniWebCompiler", "MiniWebCompiler.conf"))); // Remember the version of the application. // If we need to react on settings changes from previous application versions, here is // the place to check the version currently in the settings, before it's overwritten. //Settings.LastStartedAppVersion = FL.AppVersion; }
/// <summary> /// Returns an object for the specified interface type with a backing file store. /// </summary> /// <typeparam name="TSettings">The settings interface type to implement.</typeparam> /// <param name="directory">The directory in the application data base directory. May /// include backslashes or slashes for subdirectories.</param> /// <param name="fileName">The settings file name.</param> /// <param name="userLevel">Specifies whether a user-level path is returned instead of a /// system-level path.</param> /// <param name="readOnly">true to open the settings file in read-only mode. This prevents /// any write access to the settings and will never save the file back.</param> /// <returns></returns> /// <remarks> /// This method generates platform-default paths for Windows (%AppData% and %ProgramData%) /// and Unix/Linux systems ($HOME/.* and /etc). /// </remarks> public static TSettings NewFile <TSettings>(string directory, string fileName, bool userLevel, bool readOnly) where TSettings : class { string path; if (userLevel) { path = GetAppDataPath(directory, fileName); } else { path = GetProgramDataPath(directory, fileName); } FileSettingsStore store = new FileSettingsStore(path, readOnly); return(SettingsAdapterFactory.New <TSettings>(store)); }
/// <summary> /// Initialises the application settings. /// </summary> public static void InitializeSettings() { if (Settings != null) { return; // Already done } Settings = SettingsAdapterFactory.New <IAppSettings>( new FileSettingsStore( SettingsHelper.GetAppDataPath(@"Unclassified\TxTranslation", "TxEditor.conf"))); // Update settings format from old version FL.TraceData("LastStartedAppVersion", Settings.LastStartedAppVersion); if (string.IsNullOrEmpty(Settings.LastStartedAppVersion)) { Settings.SettingsStore.Rename("app-culture", "AppCulture"); Settings.SettingsStore.Rename("file.ask-save-upgrade", "File.AskSaveUpgrade"); Settings.SettingsStore.Rename("input.charmap", "Input.CharacterMap"); Settings.SettingsStore.Rename("view.comments", "View.ShowComments"); Settings.SettingsStore.Rename("view.monospace-font", "View.MonospaceFont"); Settings.SettingsStore.Rename("view.hidden-chars", "View.ShowHiddenChars"); Settings.SettingsStore.Rename("view.charmap", "View.ShowCharacterMap"); Settings.SettingsStore.Rename("view.font-scale", "View.FontScale"); Settings.SettingsStore.Rename("view.native-culture-names", "View.NativeCultureNames"); Settings.SettingsStore.Rename("view.suggestions", "View.ShowSuggestions"); Settings.SettingsStore.Rename("view.suggestions.horizontal-layout", "View.SuggestionsHorizontalLayout"); Settings.SettingsStore.Rename("view.suggestions.width", "View.SuggestionsWidth"); Settings.SettingsStore.Rename("view.suggestions.height", "View.SuggestionsHeight"); Settings.SettingsStore.Rename("wizard.source-code", "Wizard.SourceCode"); Settings.SettingsStore.Rename("wizard.remember-location", "Wizard.RememberLocation"); Settings.SettingsStore.Rename("wizard.hotkey-in-visual-studio-only", "Wizard.HotkeyInVisualStudioOnly"); Settings.SettingsStore.Rename("window.left", "View.MainWindowState.Left"); Settings.SettingsStore.Rename("window.top", "View.MainWindowState.Top"); Settings.SettingsStore.Rename("window.width", "View.MainWindowState.Width"); Settings.SettingsStore.Rename("window.height", "View.MainWindowState.Height"); Settings.View.MainWindowState.IsMaximized = Settings.SettingsStore.GetInt("window.state") == 2; Settings.SettingsStore.Remove("window.state"); Settings.SettingsStore.Rename("wizard.window.left", "Wizard.WindowLeft"); Settings.SettingsStore.Rename("wizard.window.top", "Wizard.WindowTop"); } // Remember the version of the application. // If we need to react on settings changes from previous application versions, here is // the place to check the version currently in the settings, before it's overwritten. Settings.LastStartedAppVersion = FL.AppVersion; }
public void FileSettingsStoreAdapter() { File.Delete("adapter.conf"); using (var store = new FileSettingsStore("adapter.conf")) { IAppSettings settings = SettingsAdapterFactory.New <IAppSettings>(store); Assert.IsNotNull(settings); CommonMethods.AdapterEmptyDefault(settings); CommonMethods.AdapterSet(settings); CommonMethods.AdapterGetTest(settings); } using (var store = new FileSettingsStore("adapter.conf")) { IAppSettings settings = SettingsAdapterFactory.New <IAppSettings>(store); Assert.IsNotNull(settings); CommonMethods.AdapterGetTest(settings); } }
public void FileSettingsStoreAdapter() { Registry.CurrentUser.DeleteSubKeyTree(@"Software\Unclassified\SettingsDemoTest\Adapter", false); using (var store = new RegistrySettingsStore(false, @"Software\Unclassified\SettingsDemoTest\Adapter")) { IAppSettings settings = SettingsAdapterFactory.New <IAppSettings>(store); Assert.IsNotNull(settings); CommonMethods.AdapterEmptyDefault(settings); CommonMethods.AdapterSet(settings); CommonMethods.AdapterGetTest(settings); } using (var store = new RegistrySettingsStore(false, @"Software\Unclassified\SettingsDemoTest\Adapter")) { IAppSettings settings = SettingsAdapterFactory.New <IAppSettings>(store); Assert.IsNotNull(settings); CommonMethods.AdapterGetTest(settings); } }