Ejemplo n.º 1
0
        /// <summary>
        /// Save configuration to specified configuration file
        /// </summary>
        /// <param name="configObject">Configuration object to be saved</param>
        /// <param name="configFileName">Configuration file name</param>
        /// <param name="sectionName">Configuration section name</param>
        protected void SaveSection(object configObject, string configFileName, string sectionName)
        {
            try
            {
                System.Configuration.Configuration config = OpenExeConfiguration(configFileName, true);

                CustomSection currentSection = (CustomSection)config.Sections[sectionName];

                if (currentSection == null)
                {
                    config.Sections.Add(sectionName, new CustomSection());
                }

                currentSection = (CustomSection)config.Sections[sectionName];

                currentSection.Data = configObject;

                currentSection.SectionInformation.ForceSave = true;

                config.Save(ConfigurationSaveMode.Minimal);
            }
            catch (ConfigurationErrorsException exc)
            {
                Log.Write(Log.Severity.ERROR,
                          System.Reflection.MethodInfo.GetCurrentMethod(),
                          " Error : {0}", exc.Message);

                throw;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Renames the section.
        /// </summary>
        /// <param name="configFileName">Name of the configuration file.</param>
        /// <param name="existingSectionName">Name of the existing section.</param>
        /// <param name="newSectionName">New name of the section.</param>
        public virtual void RenameSection(string configFileName, string existingSectionName, string newSectionName)
        {
            System.Configuration.Configuration config = OpenExeConfiguration(configFileName, false);

            if (config == null)
            {
                Log.Write(Log.Severity.WARNING,
                          System.Reflection.MethodInfo.GetCurrentMethod(),
                          "Cannot load <{0}> configuration. ", configFileName);

                return;
            }
            try
            {
                using (AssemblyVersionIgnorer versionResolver = new AssemblyVersionIgnorer())
                {
                    CustomSection currentSection = (CustomSection)config.Sections[existingSectionName];

                    if (currentSection == null)
                    {
                        Log.Write(Log.Severity.INFO,
                                  System.Reflection.MethodInfo.GetCurrentMethod(),
                                  " No relevant section in the local configuration file found. Default station options will be used.");

                        return;
                    }
                    else
                    {
                        config.Sections.Remove(existingSectionName);

                        config.Sections.Add(newSectionName, currentSection);
                    }
                }
            }
            catch (ConfigurationException exc)
            {
                Log.WriteException(exc);
            }
        }