Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            //Example how to change app.config file associated to your application. 
            //You can use the same approach for web.configs and other type of configurations
            string appConfigFilePath = string.Concat(Assembly.GetExecutingAssembly().Location, ".config");
            ConfigModificatorSettings appConfigWriterSettings = 
                new ConfigModificatorSettings("//appSettings", "//add[@key='{0}']", appConfigFilePath);
          
            string value = ConfigurationManager.AppSettings["testKey1"];
            System.Console.WriteLine("Value before modification: {0}", value);

            ConfigModificator.ChangeValueByKey(
                                                key: "testKey1", 
                                                value: "ChangedValueByModificator", 
                                                attributeForChange: "value", 
                                                configWriterSettings: appConfigWriterSettings);

            ConfigModificator.RefreshAppSettings();
            value = ConfigurationManager.AppSettings["testKey1"];
            System.Console.WriteLine("Value after modification: {0}", value);

            //Example how to change Custom XML configuration
            string carsConfigFilePath = "Cars.xml";
            ConfigModificatorSettings carsConfigWriterSettings = 
                new ConfigModificatorSettings("//cars", "//car[@name='{0}']", carsConfigFilePath);

            ConfigModificator.ChangeValueByKey(
                                               key: "BMW",
                                               value: "Mazda",
                                               attributeForChange: "name",
                                               configWriterSettings: carsConfigWriterSettings);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Changes the value by key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="attributeForChange">The attribute for change.</param>
        /// <param name="configWriterSettings">The configuration writer settings.</param>
        /// <exception cref="System.InvalidOperationException">appSettings section not found in config file.</exception>
        public static void ChangeValueByKey(string key, string value, string attributeForChange, ConfigModificatorSettings configWriterSettings)
        {
            XmlDocument doc = ConfigModificator.LoadConfigDocument(configWriterSettings.ConfigPath);
            // retrieve the root node
            XmlNode rootNode = doc.SelectSingleNode(configWriterSettings.RootNode);

            if (rootNode == null)
            {
                throw new InvalidOperationException("the root node section not found in config file.");
            }

            try
            {
                // select the element that contains the key
                XmlElement elem = (XmlElement)rootNode.SelectSingleNode(string.Format(configWriterSettings.NodeForEdit, key));
                elem.SetAttribute(attributeForChange, value);
                doc.Save(configWriterSettings.ConfigPath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Writes the setting.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="configWriterSettings">The configuration writer settings.</param>
        /// <exception cref="System.InvalidOperationException">appSettings section not found in config file.</exception>
        public static void WriteSetting(string key, string value, ConfigModificatorSettings configWriterSettings)
        {
            XmlDocument doc = ConfigModificator.LoadConfigDocument(configWriterSettings.ConfigPath);
            // retrieve appSettings node
            XmlNode rootNode = doc.SelectSingleNode(configWriterSettings.RootNode);

            if (rootNode == null)
            {
                throw new InvalidOperationException("appSettings section not found in config file.");
            }

            try
            {
                // select the 'note for edit' element that contains your key
                XmlElement elem = (XmlElement)rootNode.SelectSingleNode(string.Format(configWriterSettings.NodeForEdit, key));
                elem.FirstChild.InnerText = value;
                doc.Save(configWriterSettings.ConfigPath);
            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Writes the setting.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="configWriterSettings">The configuration writer settings.</param>
        /// <exception cref="System.InvalidOperationException">appSettings section not found in config file.</exception>
        public static void WriteSetting(string key, string value, ConfigModificatorSettings configWriterSettings)
        {
            XmlDocument doc = ConfigModificator.LoadConfigDocument(configWriterSettings.ConfigPath);
            // retrieve appSettings node
            XmlNode rootNode = doc.SelectSingleNode(configWriterSettings.RootNode);

            if (rootNode == null)
            {
                throw new InvalidOperationException("appSettings section not found in config file.");
            }

            try
            {
                // select the 'note for edit' element that contains your key
                XmlElement elem = (XmlElement)rootNode.SelectSingleNode(string.Format(configWriterSettings.NodeForEdit, key));
                elem.FirstChild.InnerText = value;
                doc.Save(configWriterSettings.ConfigPath);
            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Changes the value by key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="attributeForChange">The attribute for change.</param>
        /// <param name="configWriterSettings">The configuration writer settings.</param>
        /// <exception cref="System.InvalidOperationException">appSettings section not found in config file.</exception>
        public static void ChangeValueByKey(string key, string value, string attributeForChange, ConfigModificatorSettings configWriterSettings)
        {
            XmlDocument doc = ConfigModificator.LoadConfigDocument(configWriterSettings.ConfigPath);
            // retrieve the root node
            XmlNode rootNode = doc.SelectSingleNode(configWriterSettings.RootNode);

            if (rootNode == null)
            {
                throw new InvalidOperationException("the root node section not found in config file.");
            }

            try
            {
                // select the element that contains the key
                XmlElement elem = (XmlElement)rootNode.SelectSingleNode(string.Format(configWriterSettings.NodeForEdit, key));
                elem.SetAttribute(attributeForChange, value);
                doc.Save(configWriterSettings.ConfigPath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }