Esempio n. 1
0
        public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
        {
            _valuesCache.Clear();

            var r = new SettingsPropertyValueCollection();

            Configuration cfg = null;

            var configMap = new ExeConfigurationFileMap();

            configMap.ExeConfigFilename = SharedConfigurationPath;
            if (!WindowsIdentity.GetCurrent().IsSystem)
            {
                //Only user settings for non-SYSTEM
                configMap.LocalUserConfigFilename   = UserLocalConfigurationPath;
                configMap.RoamingUserConfigFilename = UserConfigurationPath;

                cfg = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.PerUserRoamingAndLocal);
                if (!cfg.HasFile)
                {
                    cfg = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.PerUserRoaming);
                }
            }

            if (cfg == null || !cfg.HasFile)
            {
                cfg = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
                if (!cfg.HasFile)
                {
                    throw new ApplicationException("Configuration file missing!");
                }
            }

            try
            {
                ClientSettingsSection appSettings = GetApplicationSettingsSection(cfg);
                var sets = collection.Cast <SettingsProperty>().Where(x => !IsUserSetting(x));
                ExtractSettings(sets, r, appSettings);

                ClientSettingsSection userInitialSettings = GetUserSettingsSection(cfg);
                sets = collection.Cast <SettingsProperty>().Where(x => IsUserSetting(x));
                ExtractSettings(sets, r, userInitialSettings);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error loading config");
            }

            return(r);
        }
        public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
        {
            string userName = (string)context["UserName"];

            var propertyValues = collection.Cast <SettingsProperty>()
                                 .Select(p => new SettingsPropertyValue(p))
                                 .ToArray();

            if (repository.ContainsKey(userName))
            {
                var userProperties = repository[userName];

                foreach (var pv in propertyValues)
                {
                    pv.PropertyValue = userProperties[pv.Name];
                }
            }

            var result = new SettingsPropertyValueCollection();

            foreach (var pv in propertyValues)
            {
                result.Add(pv);
            }

            return(result);
        }
Esempio n. 3
0
 private bool DoesNotContainDefaultColumns(SettingsPropertyCollection collection)
 {
     return
         (!collection.Cast <SettingsProperty>()
          .Any(
              value =>
              string.Compare(
                  value.Name, this.membershipProvider.UserIdColumn, StringComparison.OrdinalIgnoreCase)
              == 0 ||
              string.Compare(
                  value.Name,
                  this.membershipProvider.UserEmailColumn,
                  StringComparison.OrdinalIgnoreCase) == 0));
 }
Esempio n. 4
0
        /// <summary>
        /// Returns the collection of setting property values for the specified application instance and settings property group.
        /// </summary>
        /// <param name="context">A <c>System.Configuration.SettingsContext</c> that describes where the application settings property is used.</param>
        /// <param name="properties">A <c>System.Configuration.SettingsPropertyCollection</c> containing the settings property group whose values are to be retrieved.</param>
        /// <returns>A <c>System.Configuration.SettingsPropertyValueCollection</c> containing the values for the specified settings property group.</returns>
        /// <exception cref="System.Configuration.ConfigurationErrorsException">A user-scoped setting was encountered but the current configuration only supports application-scoped settings.</exception>
        public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection properties)
        {
            var settingPropertyValues = new SettingsPropertyValueCollection();

            string settingsGroupName      = Convert.ToString(context[SettingsGroupNameContextKey]);
            string connectionStringPrefix = settingsGroupName + Type.Delimiter;

            var appConfiguration         = ConfigurationManager.OpenExeConfiguration(context[SettingsClassTypeContextKey].As <Type>().Assembly.Location);
            var userConfigurationLocal   = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
            var userConfigurationRoaming = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);

            var settingsProperties     = properties.Cast <SettingsProperty>();
            var appSettingsProperties  = settingsProperties.Where(prop => prop.Attributes.Contains(typeof(ApplicationScopedSettingAttribute))).ToList();
            var userSettingsProperties = settingsProperties.Where(prop => prop.Attributes.Contains(typeof(UserScopedSettingAttribute))).ToList();

            var userSettingsPropertiesLocal   = userSettingsProperties.Where(prop => !prop.Attributes.Contains(typeof(SettingsManageabilityAttribute)));
            var userSettingsPropertiesRoaming = userSettingsProperties.Where(prop => prop.Attributes.Contains(typeof(SettingsManageabilityAttribute)));

            //Getting userSettings for 'Local' configuration:
            ApplyUserSettingsValuesToConfigurationElements(
                context,
                userConfigurationLocal,
                appConfiguration,
                settingPropertyValues,
                userSettingsPropertiesLocal);

            //Getting userSettings for 'Roaming' configuration:
            ApplyUserSettingsValuesToConfigurationElements(
                context,
                userConfigurationRoaming,
                appConfiguration,
                settingPropertyValues,
                userSettingsPropertiesRoaming);

            //Getting connectionStrings form 'appConfiguration':
            var connectionStrings = appConfiguration.ConnectionStrings.ConnectionStrings.OfType <ConnectionStringSettings>();

            ApplySettingsValuesToConfigurationElements(
                settingPropertyValues,
                connectionStrings,
                appSettingsProperties,
                (prop, configurationElement) => prop.Name == configurationElement.Name.Replace(connectionStringPrefix, string.Empty));

            //Getting application settings:
            appConfiguration.
            With(configuration => configuration.SectionGroups[ApplicationSettingsGroupName]).
            With(sectionGroup => sectionGroup.Sections[settingsGroupName].As <ClientSettingsSection>()).
            With(section => section.Settings.Cast <SettingElement>()).
            Do(applicationSettings =>
               ApplySettingsValuesToConfigurationElements(
                   settingPropertyValues,
                   applicationSettings,
                   appSettingsProperties,
                   (prop, configurationElement) => prop.Name == configurationElement.Name));

            //Adding properties that were not set (just use default values):
            var propertiesAreSet  = settingPropertyValues.Cast <SettingsPropertyValue>().Select(propValue => propValue.Name);
            var settingsAreNotSet = settingsProperties.Where(settingProperty => !propertiesAreSet.Contains(settingProperty.Name));

            settingsAreNotSet.
            ToList().
            ForEach(setting => settingPropertyValues.Add(new SettingsPropertyValue(setting)));

            return(settingPropertyValues);
        }
Esempio n. 5
0
        public static List <ProfilePropertyContract> SerializeSettingsPropertyCollection(SettingsPropertyCollection oSettingsPropertyCollection)
        {
            #region VARIABLES

            IEnumerable <SettingsProperty>   oSettingsPropertyCollectionToRead;
            List <ProfilePropertyContract>   oPropertyList;
            ProfilePropertyContract          oGenProperty;
            ProfilePropertyAttributeContract oGenAttribute;
            ICollection oPropertyAttributesValues;
            ICollection oPropertyAttributesKeys;
            int         iAttributeIndex;
            #endregion

            oPropertyList = new List <ProfilePropertyContract>();

            if (oSettingsPropertyCollection != null)
            {
                oSettingsPropertyCollectionToRead = oSettingsPropertyCollection.Cast <SettingsProperty>();

                if (oSettingsPropertyCollectionToRead != null)
                {
                    foreach (var property in oSettingsPropertyCollectionToRead)
                    {
                        oGenProperty = new ProfilePropertyContract()
                        {
                            Name         = (!string.IsNullOrEmpty(property.Name) ? (property.Name) : (string.Empty)),
                            Type         = ((property.PropertyType != null) ? (property.PropertyType.ToString()) : (typeof(string).ToString())),
                            IsReadOnly   = property.IsReadOnly,
                            DefaultValue = (property.DefaultValue != null) ? (property.DefaultValue.ToString()) : (string.Empty)
                        };

                        if (property.Attributes != null)
                        {
                            oGenProperty.Attributes   = new List <ProfilePropertyAttributeContract>();
                            oPropertyAttributesValues = property.Attributes.Values;
                            oPropertyAttributesKeys   = property.Attributes.Keys;

                            foreach (var oPropertyattributeKey  in oPropertyAttributesKeys)
                            {
                                oGenAttribute = new ProfilePropertyAttributeContract()
                                {
                                    Key = oPropertyattributeKey.ToString()
                                };
                                oGenProperty.Attributes.Add(oGenAttribute);
                            }

                            iAttributeIndex = 0;
                            foreach (var oPropertyAttributeValue in oPropertyAttributesValues)
                            {
                                if (oGenProperty.Attributes[iAttributeIndex] != null)
                                {
                                    oGenProperty.Attributes[iAttributeIndex].Value = oPropertyAttributeValue.ToString();
                                }
                                iAttributeIndex++;
                            }
                        }

                        oPropertyList.Add(oGenProperty);
                    }
                }
            }

            return(oPropertyList);
        }