private ClientSettingsSection DeclareUserSection(System.Configuration.Configuration config, string sectionName)
 {
     ConfigurationSectionGroup sectionGroup = config.GetSectionGroup("userSettings");
     if (sectionGroup == null)
     {
         sectionGroup = new UserSettingsGroup();
         config.SectionGroups.Add("userSettings", sectionGroup);
     }
     bool flag = false;
     ConfigurationSection section = sectionGroup.Sections[sectionName];
     if (section == null)
     {
         section = new ClientSettingsSection();
         flag = true;
     }
     else if (section is DefaultSection)
     {
         section = this.ReadClientSettingsSection(section);
         if (section != null)
         {
             sectionGroup.Sections.Remove(sectionName);
             flag = true;
         }
     }
     if (flag)
     {
         section.SectionInformation.AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToLocalUser;
         section.SectionInformation.RequirePermission = false;
         sectionGroup.Sections.Add(sectionName, section);
     }
     return (section as ClientSettingsSection);
 }
Ejemplo n.º 2
0
        public IConfigurationSectionGroup AddSectionGroup(string sectionGroupName)
        {
            UserSettingsGroup userGroup = new UserSettingsGroup();
            _configuration.SectionGroups.Add(sectionGroupName, userGroup);

            return new ConfigurationSectionGroupWrapper(userGroup);
        }
Ejemplo n.º 3
0
        // Declares the section handler of a given section in its section group, if a declaration isn't already
        // present.
        private static void DeclareSection(Configuration config, string sectionName)
        {
            ConfigurationSectionGroup settingsGroup = config.GetSectionGroup(UserSettingsGroupName);

            if (settingsGroup == null)
            {
                //Declare settings group
                ConfigurationSectionGroup group = new UserSettingsGroup();
                config.SectionGroups.Add(UserSettingsGroupName, group);
            }

            settingsGroup = config.GetSectionGroup(UserSettingsGroupName);

            Debug.Assert(settingsGroup != null, "Failed to declare settings group");

            if (settingsGroup != null)
            {
                ConfigurationSection section = settingsGroup.Sections[sectionName];
                if (section == null)
                {
                    section = new ClientSettingsSection();
                    section.SectionInformation.AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToLocalUser;
                    section.SectionInformation.RequirePermission  = false;
                    settingsGroup.Sections.Add(sectionName, section);
                }
            }
        }
		public void EditAfterAdd ()
		{
			Config cfg = ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.None);
			UserSettingsGroup u = new UserSettingsGroup ();
			cfg.SectionGroups.Add ("userSettings", u);
			ClientSettingsSection c = new ClientSettingsSection ();
			u.Sections.Add ("mine", c);
		}
 private void DeclareSection(System.Configuration.Configuration config, string sectionName)
 {
     if (config.GetSectionGroup("userSettings") == null)
     {
         ConfigurationSectionGroup group2 = new UserSettingsGroup();
         config.SectionGroups.Add("userSettings", group2);
     }
     ConfigurationSectionGroup sectionGroup = config.GetSectionGroup("userSettings");
     if ((sectionGroup != null) && (sectionGroup.Sections[sectionName] == null))
     {
         ConfigurationSection section = new ClientSettingsSection {
             SectionInformation = { AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToLocalUser, RequirePermission = false }
         };
         sectionGroup.Sections.Add(sectionName, section);
     }
 }
Ejemplo n.º 6
0
        private void DeclareSection(System.Configuration.Configuration config, string sectionName)
        {
            if (config.GetSectionGroup("userSettings") == null)
            {
                ConfigurationSectionGroup group2 = new UserSettingsGroup();
                config.SectionGroups.Add("userSettings", group2);
            }
            ConfigurationSectionGroup sectionGroup = config.GetSectionGroup("userSettings");

            if ((sectionGroup != null) && (sectionGroup.Sections[sectionName] == null))
            {
                ConfigurationSection section = new ClientSettingsSection {
                    SectionInformation = { AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToLocalUser, RequirePermission = false }
                };
                sectionGroup.Sections.Add(sectionName, section);
            }
        }
        // Declares the section handler of a given section in its section group, if a declaration isn't already
        // present. 
        private void DeclareSection(Configuration config, string sectionName) {
            ConfigurationSectionGroup settingsGroup = config.GetSectionGroup(UserSettingsGroupName);
            
            if (settingsGroup == null) {
                //Declare settings group
                ConfigurationSectionGroup group = new UserSettingsGroup();
                config.SectionGroups.Add(UserSettingsGroupName, group);
            }

            settingsGroup = config.GetSectionGroup(UserSettingsGroupName);

            Debug.Assert(settingsGroup != null, "Failed to declare settings group");

            if (settingsGroup != null) {
                ConfigurationSection section = settingsGroup.Sections[sectionName];
                if (section == null) {
                    section = new ClientSettingsSection();
                    section.SectionInformation.AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToLocalUser;
                    section.SectionInformation.RequirePermission = false;
                    settingsGroup.Sections.Add(sectionName, section);
                }
            }
        }
        private void SaveProperties(ExeConfigurationFileMap exeMap, SettingsPropertyValueCollection collection, ConfigurationUserLevel level, SettingsContext context, bool checkUserLevel)
        {
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(exeMap, level);

            UserSettingsGroup userGroup = config.GetSectionGroup("userSettings") as UserSettingsGroup;
            bool isRoaming = (level == ConfigurationUserLevel.PerUserRoaming);

#if true // my reimplementation
            if (userGroup == null)
            {
                userGroup = new UserSettingsGroup();
                config.SectionGroups.Add("userSettings", userGroup);
                ApplicationSettingsBase asb = context.CurrentSettings;
                ClientSettingsSection   cs  = new ClientSettingsSection();
                string class_name           = NormalizeInvalidXmlChars((asb != null ? asb.GetType() : typeof(ApplicationSettingsBase)).FullName);
                userGroup.Sections.Add(class_name, cs);
            }

            bool hasChanges = false;

            foreach (ConfigurationSection section in userGroup.Sections)
            {
                ClientSettingsSection userSection = section as ClientSettingsSection;
                if (userSection == null)
                {
                    continue;
                }

                foreach (SettingsPropertyValue value in collection)
                {
                    if (checkUserLevel && value.Property.Attributes.Contains(typeof(SettingsManageabilityAttribute)) != isRoaming)
                    {
                        continue;
                    }
                    // The default impl does not save the ApplicationScopedSetting properties
                    if (value.Property.Attributes.Contains(typeof(ApplicationScopedSettingAttribute)))
                    {
                        continue;
                    }

                    hasChanges = true;
                    SettingElement element = userSection.Settings.Get(value.Name);
                    if (element == null)
                    {
                        element = new SettingElement(value.Name, value.Property.SerializeAs);
                        userSection.Settings.Add(element);
                    }
                    if (element.Value.ValueXml == null)
                    {
                        element.Value.ValueXml = new XmlDocument().CreateElement("value");
                    }
                    switch (value.Property.SerializeAs)
                    {
                    case SettingsSerializeAs.Xml:
                        element.Value.ValueXml.InnerXml = (value.SerializedValue as string) ?? string.Empty;
                        break;

                    case SettingsSerializeAs.String:
                        element.Value.ValueXml.InnerText = value.SerializedValue as string;
                        break;

                    case SettingsSerializeAs.Binary:
                        element.Value.ValueXml.InnerText = value.SerializedValue != null?Convert.ToBase64String(value.SerializedValue as byte []) : string.Empty;

                        break;

                    default:
                        throw new NotImplementedException();
                    }
                }
            }
            if (hasChanges)
            {
                config.Save(ConfigurationSaveMode.Minimal, true);
            }
#else // original impl. - likely buggy to miss some properties to save
            foreach (ConfigurationSection configSection in userGroup.Sections)
            {
                ClientSettingsSection userSection = configSection as ClientSettingsSection;
                if (userSection != null)
                {
/*
 *                                      userSection.Settings.Clear();
 *
 *                                      foreach (SettingsPropertyValue propertyValue in collection)
 *                                      {
 *                                              if (propertyValue.IsDirty)
 *                                              {
 *                                                      SettingElement element = new SettingElement(propertyValue.Name, SettingsSerializeAs.String);
 *                                                      element.Value.ValueXml = new XmlDocument();
 *                                                      element.Value.ValueXml.InnerXml = (string)propertyValue.SerializedValue;
 *                                                      userSection.Settings.Add(element);
 *                                              }
 *                                      }
 */
                    foreach (SettingElement element in userSection.Settings)
                    {
                        if (collection [element.Name] != null)
                        {
                            if (collection [element.Name].Property.Attributes.Contains(typeof(SettingsManageabilityAttribute)) != isRoaming)
                            {
                                continue;
                            }

                            element.SerializeAs             = SettingsSerializeAs.String;
                            element.Value.ValueXml.InnerXml = (string)collection [element.Name].SerializedValue;                                ///Value = XmlElement
                        }
                    }
                }
            }
            config.Save(ConfigurationSaveMode.Minimal, true);
#endif
        }
		private void SaveProperties (ExeConfigurationFileMap exeMap, SettingsPropertyValueCollection collection, ConfigurationUserLevel level, SettingsContext context, bool checkUserLevel)
		{
			Configuration config = ConfigurationManager.OpenMappedExeConfiguration (exeMap, level);
			
			UserSettingsGroup userGroup = config.GetSectionGroup ("userSettings") as UserSettingsGroup;
			bool isRoaming = (level == ConfigurationUserLevel.PerUserRoaming);

#if true // my reimplementation

			if (userGroup == null) {
				userGroup = new UserSettingsGroup ();
				config.SectionGroups.Add ("userSettings", userGroup);
				ApplicationSettingsBase asb = context.CurrentSettings;
				ClientSettingsSection cs = new ClientSettingsSection ();
				string class_name = NormalizeInvalidXmlChars ((asb != null ? asb.GetType () : typeof (ApplicationSettingsBase)).FullName);
				userGroup.Sections.Add (class_name, cs);
			}

			bool hasChanges = false;

			foreach (ConfigurationSection section in userGroup.Sections) {
				ClientSettingsSection userSection = section as ClientSettingsSection;
				if (userSection == null)
					continue;

				foreach (SettingsPropertyValue value in collection) {
					if (checkUserLevel && value.Property.Attributes.Contains (typeof (SettingsManageabilityAttribute)) != isRoaming)
						continue;
					// The default impl does not save the ApplicationScopedSetting properties
					if (value.Property.Attributes.Contains (typeof (ApplicationScopedSettingAttribute)))
						continue;

					hasChanges = true;
					SettingElement element = userSection.Settings.Get (value.Name);
					if (element == null) {
						element = new SettingElement (value.Name, value.Property.SerializeAs);
						userSection.Settings.Add (element);
					}
					if (element.Value.ValueXml == null)
						element.Value.ValueXml = new XmlDocument ().CreateElement ("value");
					switch (value.Property.SerializeAs) {
					case SettingsSerializeAs.Xml:
						element.Value.ValueXml.InnerXml = (value.SerializedValue as string) ?? string.Empty;
						break;
					case SettingsSerializeAs.String:
						element.Value.ValueXml.InnerText = value.SerializedValue as string;
						break;
					case SettingsSerializeAs.Binary:
						element.Value.ValueXml.InnerText = value.SerializedValue != null ? Convert.ToBase64String (value.SerializedValue as byte []) : string.Empty;
						break;
					default:
						throw new NotImplementedException ();
					}
				}
			}
			if (hasChanges)
				config.Save (ConfigurationSaveMode.Minimal, true);

#else // original impl. - likely buggy to miss some properties to save

			foreach (ConfigurationSection configSection in userGroup.Sections)
			{
				ClientSettingsSection userSection = configSection as ClientSettingsSection;
				if (userSection != null)
				{
/*
					userSection.Settings.Clear();

					foreach (SettingsPropertyValue propertyValue in collection)
					{
						if (propertyValue.IsDirty)
						{
							SettingElement element = new SettingElement(propertyValue.Name, SettingsSerializeAs.String);
							element.Value.ValueXml = new XmlDocument();
							element.Value.ValueXml.InnerXml = (string)propertyValue.SerializedValue;
							userSection.Settings.Add(element);
						}
					}
*/
					foreach (SettingElement element in userSection.Settings)
					{
						if (collection [element.Name] != null) {
							if (collection [element.Name].Property.Attributes.Contains (typeof (SettingsManageabilityAttribute)) != isRoaming)
								continue;

							element.SerializeAs = SettingsSerializeAs.String;
							element.Value.ValueXml.InnerXml = (string) collection [element.Name].SerializedValue;	///Value = XmlElement
						}
					}
 
				}
			}
			config.Save (ConfigurationSaveMode.Minimal, true);
#endif
		}
Ejemplo n.º 10
0
        private void SaveProperties(ExeConfigurationFileMap exeMap, SettingsPropertyValueCollection collection, ConfigurationUserLevel level, SettingsContext context, bool checkUserLevel)
        {
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(exeMap, level);

            UserSettingsGroup userGroup = config.GetSectionGroup("userSettings") as UserSettingsGroup;
            bool isRoaming = (level == ConfigurationUserLevel.PerUserRoaming);

            if (userGroup == null)
            {
                userGroup = new UserSettingsGroup();
                config.SectionGroups.Add("userSettings", userGroup);
            }
            ApplicationSettingsBase asb       = context.CurrentSettings;
            string class_name                 = NormalizeInvalidXmlChars((asb != null ? asb.GetType() : typeof(ApplicationSettingsBase)).FullName);
            ClientSettingsSection userSection = null;
            ConfigurationSection  cnf         = userGroup.Sections.Get(class_name);

            userSection = cnf as ClientSettingsSection;
            if (userSection == null)
            {
                userSection = new ClientSettingsSection();
                userGroup.Sections.Add(class_name, userSection);
            }

            bool hasChanges = false;

            if (userSection == null)
            {
                return;
            }

            foreach (SettingsPropertyValue value in collection)
            {
                if (checkUserLevel && value.Property.Attributes.Contains(typeof(SettingsManageabilityAttribute)) != isRoaming)
                {
                    continue;
                }
                // The default impl does not save the ApplicationScopedSetting properties
                if (value.Property.Attributes.Contains(typeof(ApplicationScopedSettingAttribute)))
                {
                    continue;
                }

                hasChanges = true;
                SettingElement element = userSection.Settings.Get(value.Name);
                if (element == null)
                {
                    element = new SettingElement(value.Name, value.Property.SerializeAs);
                    userSection.Settings.Add(element);
                }
                if (element.Value.ValueXml == null)
                {
                    element.Value.ValueXml = new XmlDocument().CreateElement("value");
                }
                switch (value.Property.SerializeAs)
                {
                case SettingsSerializeAs.Xml:
                    element.Value.ValueXml.InnerXml = StripXmlHeader(value.SerializedValue as string);
                    break;

                case SettingsSerializeAs.String:
                    element.Value.ValueXml.InnerText = value.SerializedValue as string;
                    break;

                case SettingsSerializeAs.Binary:
                    element.Value.ValueXml.InnerText = value.SerializedValue != null?Convert.ToBase64String(value.SerializedValue as byte []) : string.Empty;

                    break;

                default:
                    throw new NotImplementedException();
                }
            }
            if (hasChanges)
            {
                config.Save(ConfigurationSaveMode.Minimal, true);
            }
        }
Ejemplo n.º 11
0
       static ConfigurationSectionGroup AddSectionGroup(Configuration pConfiguration, String pSectionGroupName)
       {

           ConfigurationSectionGroup wConfigurationSectionGroup = null;
           if (pSectionGroupName == "applicationSettings")
               wConfigurationSectionGroup = new ApplicationSettingsGroup();
           if (pSectionGroupName == "userSettingsGroup")
               wConfigurationSectionGroup = new UserSettingsGroup();

          pConfiguration.SectionGroups.Add(pSectionGroupName, wConfigurationSectionGroup);
          return wConfigurationSectionGroup;
       }
Ejemplo n.º 12
0
        /// <summary>
        /// Gets the <see cref="SettingElementCollection"/> for the specified section name within
        /// the specified configuration.
        /// </summary>
        /// <param name="config">The <see cref="Configuration"/> object.</param>
        /// <param name="sectionName">The settings section name.</param>
        /// <returns>
        /// A <see cref="SettingElementCollection"/> for the section, or an empty section if not found.
        /// </returns>
        private static SettingElementCollection GetSettingElementCollection(Configuration config, string sectionName)
        {
            var userSettings = config.GetSectionGroup("userSettings");
            if (userSettings == null)
            {
                userSettings = new UserSettingsGroup();
                config.SectionGroups.Add("userSettings", userSettings);
            }

            var section = userSettings.Sections.Get(sectionName) as ClientSettingsSection;
            if (section == null)
            {
                section = new ClientSettingsSection();
                userSettings.Sections.Add(sectionName, section);
            }

            return section.Settings;
        }
		private void SaveProperties (ExeConfigurationFileMap exeMap, SettingsPropertyValueCollection collection, ConfigurationUserLevel level, SettingsContext context, bool checkUserLevel)
		{
			Configuration config = ConfigurationManager.OpenMappedExeConfiguration (exeMap, level);
			
			UserSettingsGroup userGroup = config.GetSectionGroup ("userSettings") as UserSettingsGroup;
			bool isRoaming = (level == ConfigurationUserLevel.PerUserRoaming);

			if (userGroup == null) {
				userGroup = new UserSettingsGroup ();
				config.SectionGroups.Add ("userSettings", userGroup);
			}
			ApplicationSettingsBase asb = context.CurrentSettings;
			string class_name = NormalizeInvalidXmlChars ((asb != null ? asb.GetType () : typeof (ApplicationSettingsBase)).FullName);
			ClientSettingsSection userSection = null;
			ConfigurationSection cnf = userGroup.Sections.Get (class_name);
			userSection = cnf as ClientSettingsSection;
			if (userSection == null) {
				userSection = new ClientSettingsSection ();
				userGroup.Sections.Add (class_name, userSection);
			}

			bool hasChanges = false;

			if (userSection == null)
				return;

			foreach (SettingsPropertyValue value in collection) {
				if (checkUserLevel && value.Property.Attributes.Contains (typeof (SettingsManageabilityAttribute)) != isRoaming)
					continue;
				// The default impl does not save the ApplicationScopedSetting properties
				if (value.Property.Attributes.Contains (typeof (ApplicationScopedSettingAttribute)))
					continue;

				hasChanges = true;
				SettingElement element = userSection.Settings.Get (value.Name);
				if (element == null) {
					element = new SettingElement (value.Name, value.Property.SerializeAs);
					userSection.Settings.Add (element);
				}
				if (element.Value.ValueXml == null)
					element.Value.ValueXml = new XmlDocument ().CreateElement ("value");
				switch (value.Property.SerializeAs) {
				case SettingsSerializeAs.Xml:
					element.Value.ValueXml.InnerXml = StripXmlHeader (value.SerializedValue as string);
					break;
				case SettingsSerializeAs.String:
					element.Value.ValueXml.InnerText = value.SerializedValue as string;
					break;
				case SettingsSerializeAs.Binary:
					element.Value.ValueXml.InnerText = value.SerializedValue != null ? Convert.ToBase64String (value.SerializedValue as byte []) : string.Empty;
					break;
				default:
					throw new NotImplementedException ();
				}
			}
			if (hasChanges)
				config.Save (ConfigurationSaveMode.Minimal, true);
		}
Ejemplo n.º 14
0
        } // SaveAs

        // ----------------------------------------------------------------------
        public void Import(UserConfig importUserConfig, bool overwriteSettings)
        {
            if (importUserConfig == null)
            {
                throw new ArgumentNullException("importUserConfig");
            }

            foreach (ConfigurationSectionGroup importSectionGroup in importUserConfig.configuration.SectionGroups)
            {
                UserSettingsGroup importUserSettingsGroup = importSectionGroup as UserSettingsGroup;
                if (importUserSettingsGroup == null)
                {
                    continue;
                }

                UserSettingsGroup userSettingsGroup = configuration.SectionGroups[importSectionGroup.Name] as UserSettingsGroup;
                if (userSettingsGroup == null)
                {
                    userSettingsGroup = new UserSettingsGroup();
                    configuration.SectionGroups.Add(importSectionGroup.Name, userSettingsGroup);
                }

                foreach (ConfigurationSection importSection in importSectionGroup.Sections)
                {
                    ClientSettingsSection importClientSettingsSection = importSection as ClientSettingsSection;
                    if (importClientSettingsSection == null)
                    {
                        continue;
                    }

                    ClientSettingsSection clientSettingsSection = userSettingsGroup.Sections[importSection.SectionInformation.Name] as ClientSettingsSection;
                    if (clientSettingsSection == null)
                    {
                        clientSettingsSection = new ClientSettingsSection();
                        userSettingsGroup.Sections.Add(importSection.SectionInformation.Name, clientSettingsSection);
                    }

                    foreach (SettingElement importSettingElement in importClientSettingsSection.Settings)
                    {
                        bool newSetting = false;

                        SettingElement settingElement = clientSettingsSection.Settings.Get(importSettingElement.Name);
                        if (settingElement == null)
                        {
                            newSetting = true;
                            settingElement = new SettingElement();
                            settingElement.Name = importSettingElement.Name;
                            settingElement.SerializeAs = importSettingElement.SerializeAs;
                            clientSettingsSection.Settings.Add(settingElement);
                        }

                        if (!newSetting && !overwriteSettings)
                        {
                            continue;
                        }

                        SettingValueElement settingValueElement = new SettingValueElement();
                        settingValueElement.ValueXml = importSettingElement.Value.ValueXml;
                        settingElement.SerializeAs = importSettingElement.SerializeAs;
                        settingElement.Value = settingValueElement;
                        clientSettingsSection.Settings.Add(settingElement);
                    }
                }
            }
        } // Import
		public void EditBeforeAdd ()
		{
			UserSettingsGroup u = new UserSettingsGroup ();
			ClientSettingsSection c = new ClientSettingsSection ();
			u.Sections.Add ("mine", c);
		}