/// <summary>Adds the <paramref name="defAttrib"/> as a default value in the configuration.</summary> /// <param name="defAttrib">The <see cref="ConfigurationAttribute"/> to add to the default collection.</param> /// <remarks><para>This method is mainly to be used during application startup to initialize the default values for associated strings. May be of use to code that deals with /// third-party code that is loaded later in application runtime.</para> /// <para>This method does <strong>not</strong> add <see cref="ConfigurationAttribute"/>s to specific configurations, rather, is added to an application-wide /// storage of these default associations.</para></remarks> /// <returns>A value indicating whether or not the <paramref name="defAttrib"/> was entered. <strong>true</strong> is returned if the key of the <paramref name="defAttrib"/> previously did not exist, and /// was added. <strong>false</strong> if the key did exist, and the <paramref name="defAttrib"/> was not added.</returns> /// <example> /// <code title="Example" description="Code that demonstrates the use of creating default values for strings later in application runtime." lang="C#"> /// { /// //Using fictitious code, or pseudocode to demonstrate usage. /// var plugin = PluginFactory.LoadFromFile("plugin.dll"); /// var configList = plugin.GetConfigAttributes(); /// /// foreach(var attrib in configList) /// { /// if(!Configuration.Global.AddDefault(attrib)) /// { /// Console.WriteLine("Could not set default value for key "+attrib.Key); /// } else /// { /// Console.WriteLine("Set default value of "+attrib.Value+" to "+attrib.Key); /// } /// } /// }</code> /// </example> public static bool AddDefault(ConfigurationAttribute defAttrib) { if (compiledConfiguration.ContainsKey(defAttrib.Key)) { return(false); } compiledConfiguration[defAttrib.Key] = defAttrib; return(true); }
/// <summary>Initializes the configuration class for use.</summary> /// <param name="appDataPath">The absolute path to the desired configuration folder.</param> /// <remarks>Initializes the application-wide configuration by reading <see cref="ConfigurationAttribute"/> meta-data in all loaded assemblies. Sets the folder where configuration files are /// kept to <paramref name="appDataPath"/>. This path is accessed as a configuration key: "configuration_folder".</remarks> /// <example> /// <code title="C#" description="Code that demonstrates how to initialize a configuration on application startup." lang="C#"> /// class Application /// { /// static void Main(string args[]) /// { /// //Setting the configuration folder to the root of the drive under "conf" /// Configuration.Initialize("C:\\conf"); /// } /// } /// </code> /// </example> public static void Initialize(string appDataPath) { compiledConfiguration = new Dictionary <string, ConfigurationAttribute>(); LoadConfigurationDefaults(); if (appDataPath.Last() != '\\') { appDataPath += '\\'; } var rootAttrib = new ConfigurationAttribute("configuration_folder", appDataPath); rootAttrib.DisplayName = "User Configuration Folder"; rootAttrib.Description = "The absolute folder path of the directory that will contain all saved configurations."; compiledConfiguration.Add(rootAttrib.Key, rootAttrib); globalInstance = new Configuration(); globalInstance.Name = "global"; globalInstance.LoadConfigFile(); }