Inheritance: SettingsProvider, IApplicationSettingsProvider
        public static void RemovePreviousConfigFile(LocalFileSettingsProvider lfsp)
        {
            if (lfsp == null) return;

            Type t = lfsp.GetType();

            object o = t.InvokeMember("GetPreviousConfigFileName",
                BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase,
                null,
                lfsp,
                new object[] { true }
            );

            string configFile = o as string;
            if (!String.IsNullOrEmpty(configFile) && File.Exists(configFile))
            {
                Directory.Delete(Path.GetDirectoryName(configFile), true);
            }

            o = t.InvokeMember("GetPreviousConfigFileName",
                BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase,
                null,
                lfsp,
                new object[] { false }
            );
            configFile = o as string;
            if (!String.IsNullOrEmpty(configFile) && File.Exists(configFile))
            {
                Directory.Delete(Path.GetDirectoryName(configFile), true);
            }
        }
        public static SettingsPropertyValueCollection GetSharedPropertyValues(LocalFileSettingsProvider provider, SettingsContext context, SettingsPropertyCollection properties, string currentExeConfigFilename = null)
        {
			var settingsClass = (Type)context["SettingsClassType"];

            var systemConfiguration = String.IsNullOrEmpty(currentExeConfigFilename)
                              ? SystemConfigurationHelper.GetExeConfiguration()
                              : SystemConfigurationHelper.GetExeConfiguration(currentExeConfigFilename);

        	var storedValues = systemConfiguration.GetSettingsValues(settingsClass);

			// Create new collection of values
			var values = new SettingsPropertyValueCollection();

			foreach (SettingsProperty setting in properties)
			{
				var value = new SettingsPropertyValue(setting)
				{
					SerializedValue = storedValues.ContainsKey(setting.Name) ? storedValues[setting.Name] : null,
					IsDirty = false
				};

				// use the stored value, or set the SerializedValue to null, which tells .NET to use the default value
				values.Add(value);
			}

			return values;
		}
Example #3
0
        /// <summary>
        /// Updates application settings to reflect a more recent installation of the application.
        /// </summary>
        public override void Upgrade()
        {
            var oldSettingsProvider = new LocalFileSettingsProvider();
            var oldPropertyValues = oldSettingsProvider.GetPropertyValues(Context, Properties);

            foreach (SettingsPropertyValue oldPropertyValue in oldPropertyValues)
            {
                if (!Equals(this[oldPropertyValue.Name], oldPropertyValue.PropertyValue))
                {
                    this[oldPropertyValue.Name] = oldPropertyValue.PropertyValue;
                }
            }
        }
        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            try
            {
                _localFileSettingProvider = new LocalFileSettingsProvider();
            }
            catch (Exception)
            {
            
            }

            base.Initialize(ApplicationName, config);
        }
		public void Properties ()
		{
			LocalFileSettingsProvider prov = new LocalFileSettingsProvider ();

			// defaults, uninitialized
			Assert.IsNull (prov.Name, "A1");
			Assert.AreEqual ("", prov.ApplicationName, "A2");

			prov.ApplicationName = "foo";
			Assert.AreEqual ("foo", prov.ApplicationName, "A3");

			prov.ApplicationName = null;
			Assert.IsNull (prov.ApplicationName, "A4");
		}
		} // ApplicationSettings

		// ----------------------------------------------------------------------
		public ApplicationSettings( string settingsKey, object obj ) :
			base( settingsKey )
		{
			settings = new SettingCollection( this );

			// provider
			defaultProvider = new LocalFileSettingsProvider();
			defaultProvider.Initialize( "LocalFileSettingsProvider", new NameValueCollection() );
			base.Providers.Add( defaultProvider );

			// upgrade
			upgradeSettings = new ValueSetting( UpgradeSettingsKey, typeof( bool ), true );
			UseAutoUpgrade = true;

			if ( obj != null )
			{
				Settings.AddAll( obj );
			}
		} // ApplicationSettings
        public static void UpgradeSharedPropertyValues(LocalFileSettingsProvider provider, SettingsContext context, SettingsPropertyCollection properties, string previousExeConfigFilename, string currentExeConfigFilename = null)
        {
            SettingsPropertyValueCollection currentValues = GetSharedPropertyValues(provider, context, properties, currentExeConfigFilename);
            SettingsPropertyValueCollection previousValues = GetPreviousSharedPropertyValues(provider, context, properties, previousExeConfigFilename);

			foreach (SettingsProperty property in properties)
			{
				SettingsPropertyValue previousValue = previousValues[property.Name];
				if (previousValue != null)
				{
					SettingsPropertyValue currentValue = currentValues[property.Name];
					if (currentValue == null)
						currentValues.Add(previousValue);
					else
						currentValue.SerializedValue = previousValue.SerializedValue;
				}
			}

			SetSharedPropertyValues(provider, context, currentValues, currentExeConfigFilename);
        }
		public void Initialized ()
		{
			LocalFileSettingsProvider prov = new LocalFileSettingsProvider ();

			prov.Initialize (null, null);

			// defaults, uninitialized
			Assert.AreEqual ("LocalFileSettingsProvider", prov.Name, "A1");
			Assert.AreEqual ("", prov.ApplicationName, "A2");

			prov = new LocalFileSettingsProvider ();
			NameValueCollection nv = new NameValueCollection ();
			nv.Add ("applicationName", "appName");

			prov.Initialize ("hi", nv);
			// As these lines below shows, Initialize() behavior is unpredictable. Here I just comment out them and fix run-test-ondotnet tests.
			//Assert.AreEqual ("hi", prov.Name, "A3");
			//Assert.AreEqual ("hi", prov.Description, "A3.5");
			//Assert.AreEqual ("", prov.ApplicationName, "A4");
		}
        public static SettingsPropertyValueCollection GetPreviousSharedPropertyValues(LocalFileSettingsProvider provider, SettingsContext context, SettingsPropertyCollection properties, string previousExeConfigFilename)
        {
        	var values = new SettingsPropertyValueCollection();
			if (String.IsNullOrEmpty(previousExeConfigFilename))
				return values;

        	var settingsClass = (Type)context["SettingsClassType"];
			var previousValues = new ConfigurationFileReader(previousExeConfigFilename).GetSettingsValues(settingsClass);
			if (previousValues == null)
				return values;

			foreach (var value in previousValues)
			{
				var property = properties[value.Key];
				if (property == null)
					continue;

				var settingsValue = new SettingsPropertyValue(property) { SerializedValue = value.Value, IsDirty = false };
				values.Add(settingsValue);
			}

			return values;
        }
 public ExtendedLocalFileSettingsProvider(LocalFileSettingsProvider provider)
 {
     _provider = provider;
     _appName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
 }
		void CreateSettingsProperty (PropertyInfo prop, SettingsPropertyCollection properties, ref LocalFileSettingsProvider local_provider)
		{
			SettingsAttributeDictionary dict = new SettingsAttributeDictionary ();
			SettingsProvider provider = null;
			object defaultValue = null;
			SettingsSerializeAs serializeAs = SettingsSerializeAs.String;
			bool explicitSerializeAs = false;

			foreach (Attribute a in prop.GetCustomAttributes (false)) {
				/* the attributes we handle natively here */
				if (a is SettingsProviderAttribute) {
					Type provider_type = Type.GetType (((SettingsProviderAttribute)a).ProviderTypeName);
					provider = (SettingsProvider) Activator.CreateInstance (provider_type);
					provider.Initialize (null, null);
				}
				else if (a is DefaultSettingValueAttribute) {
					defaultValue = ((DefaultSettingValueAttribute)a).Value;
				}
				else if (a is SettingsSerializeAsAttribute) {
					serializeAs = ((SettingsSerializeAsAttribute)a).SerializeAs;
					explicitSerializeAs = true;
				}
				else if (a is ApplicationScopedSettingAttribute ||
					 a is UserScopedSettingAttribute) {
					dict.Add (a.GetType(), a);
				}
				else {
					dict.Add (a.GetType(), a);
				}
			}

			if (!explicitSerializeAs) {
				// DefaultValue is a string and if we can't convert from string to the 
				// property type then the only other option left is for the string to 
				// be XML.
				//
				TypeConverter converter = TypeDescriptor.GetConverter (prop.PropertyType);
				if (converter != null && 
				    (!converter.CanConvertFrom (typeof (string)) || 
				     !converter.CanConvertTo (typeof (string))))
					serializeAs = SettingsSerializeAs.Xml;
			}

			SettingsProperty setting =
				new SettingsProperty (prop.Name, prop.PropertyType, provider, false /* XXX */,
						      defaultValue /* XXX always a string? */, serializeAs, dict,
						      false, false);


			if (providerService != null)
				setting.Provider = providerService.GetSettingsProvider (setting);

			if (provider == null) {
				if (local_provider == null) {
					local_provider = new LocalFileSettingsProvider ();
					local_provider.Initialize (null, null);
				}
				setting.Provider = local_provider;
				// .NET ends up to set this to providers.
				provider = local_provider;
			}

			if (provider != null) {
				/* make sure we're using the same instance of a
				   given provider across multiple properties */
				SettingsProvider p = Providers[provider.Name];
				if (p != null)
					setting.Provider = p;
			}

			properties.Add (setting);

			if (setting.Provider != null && Providers [setting.Provider.Name] == null)
				Providers.Add (setting.Provider);
		}
        void CreateSettingsProperty(PropertyInfo prop, SettingsPropertyCollection properties, ref SettingsProvider local_provider)
        {
            SettingsAttributeDictionary dict     = new SettingsAttributeDictionary();
            SettingsProvider            provider = null;
            object defaultValue             = null;
            SettingsSerializeAs serializeAs = SettingsSerializeAs.String;
            bool explicitSerializeAs        = false;

            foreach (Attribute a in prop.GetCustomAttributes(false))
            {
                /* the attributes we handle natively here */
                if (a is SettingsProviderAttribute)
                {
                    var  providerTypeName = ((SettingsProviderAttribute)a).ProviderTypeName;
                    Type provider_type    = Type.GetType(providerTypeName);
                    if (provider_type == null)                      // Type failed to find the type by name
                    {
                        var typeNameParts = providerTypeName.Split('.');
                        if (typeNameParts.Length > 1)                          //Load the assembly that providerTypeName claims
                        {
                            var assy = Assembly.Load(typeNameParts[0]);
                            if (assy != null)
                            {
                                provider_type = assy.GetType(providerTypeName);                                 //try to get the type from that Assembly
                            }
                        }
                    }
                    provider = (SettingsProvider)Activator.CreateInstance(provider_type);
                    provider.Initialize(null, null);
                }
                else if (a is DefaultSettingValueAttribute)
                {
                    defaultValue = ((DefaultSettingValueAttribute)a).Value;
                }
                else if (a is SettingsSerializeAsAttribute)
                {
                    serializeAs         = ((SettingsSerializeAsAttribute)a).SerializeAs;
                    explicitSerializeAs = true;
                }
                else if (a is ApplicationScopedSettingAttribute ||
                         a is UserScopedSettingAttribute)
                {
                    dict.Add(a.GetType(), a);
                }
                else
                {
                    dict.Add(a.GetType(), a);
                }
            }

            if (!explicitSerializeAs)
            {
                // DefaultValue is a string and if we can't convert from string to the
                // property type then the only other option left is for the string to
                // be XML.
                //
                TypeConverter converter = TypeDescriptor.GetConverter(prop.PropertyType);
                if (converter != null &&
                    (!converter.CanConvertFrom(typeof(string)) ||
                     !converter.CanConvertTo(typeof(string))))
                {
                    serializeAs = SettingsSerializeAs.Xml;
                }
            }

            SettingsProperty setting =
                new SettingsProperty(prop.Name, prop.PropertyType, provider, false /* XXX */,
                                     defaultValue /* XXX always a string? */, serializeAs, dict,
                                     false, false);


            if (providerService != null)
            {
                setting.Provider = providerService.GetSettingsProvider(setting);
            }

            if (provider == null)
            {
                if (local_provider == null)
                {
                    local_provider = new LocalFileSettingsProvider() as SettingsProvider;
                    local_provider.Initialize(null, null);
                }
                setting.Provider = local_provider;
                // .NET ends up to set this to providers.
                provider = local_provider;
            }

            if (provider != null)
            {
                /* make sure we're using the same instance of a
                 * given provider across multiple properties */
                SettingsProvider p = Providers[provider.Name];
                if (p != null)
                {
                    setting.Provider = p;
                }
            }

            properties.Add(setting);

            if (setting.Provider != null && Providers [setting.Provider.Name] == null)
            {
                Providers.Add(setting.Provider);
            }
        }
        public static void SetSharedPropertyValues(LocalFileSettingsProvider provider, SettingsContext context, SettingsPropertyValueCollection values, string currentExeConfigFilename)
        {
            var valuesToStore = new Dictionary<string, string>();
			foreach (SettingsPropertyValue value in values)
			{
				if (value.SerializedValue != null)
					valuesToStore[value.Name] = (string)value.SerializedValue;
			}

            var systemConfiguration = String.IsNullOrEmpty(currentExeConfigFilename)
                                          ? SystemConfigurationHelper.GetExeConfiguration()
                                          : SystemConfigurationHelper.GetExeConfiguration(currentExeConfigFilename);

        	var settingsClass = (Type)context["SettingsClassType"];
            systemConfiguration.PutSettingsValues(settingsClass, valuesToStore);

            foreach (SettingsPropertyValue value in values)
                value.IsDirty = false;
        }
 public ExtendedLocalFileSettingsProvider(LocalFileSettingsProvider provider)
 {
     _provider = provider;
 }
		public void GetUserScopedPropertyValues ()
		{
			SettingsAttributeDictionary dict = new SettingsAttributeDictionary ();
			UserScopedSettingAttribute attr = new UserScopedSettingAttribute ();
			dict.Add (attr.GetType(), attr);

			LocalFileSettingsProvider prov = new LocalFileSettingsProvider ();
			SettingsContext ctx = new SettingsContext ();
			SettingsProperty p = new SettingsProperty ("property",
								   typeof (int),
								   prov,
								   false,
								   10,
								   SettingsSerializeAs.Binary,
								   dict,
								   false,
								   false);
			SettingsPropertyCollection col = new SettingsPropertyCollection ();
			SettingsPropertyValueCollection vals;

			col.Add (p);

			prov.Initialize (null, null);

			vals = prov.GetPropertyValues (ctx, col);
			Assert.IsNotNull (vals, "A1");
			Assert.AreEqual (1, vals.Count, "A2");
		}
Example #16
0
        void CreateSettingsProperty(PropertyInfo prop, SettingsPropertyCollection properties, ref LocalFileSettingsProvider local_provider)
        {
            SettingsAttributeDictionary dict     = new SettingsAttributeDictionary();
            SettingsProvider            provider = null;
            object defaultValue             = null;
            SettingsSerializeAs serializeAs = SettingsSerializeAs.String;

            foreach (Attribute a in prop.GetCustomAttributes(false))
            {
                /* the attributes we handle natively here */
                if (a is SettingsProviderAttribute)
                {
                    Type provider_type = Type.GetType(((SettingsProviderAttribute)a).ProviderTypeName);
                    provider = (SettingsProvider)Activator.CreateInstance(provider_type);
                    provider.Initialize(null, null);
                }
                else if (a is DefaultSettingValueAttribute)
                {
                    defaultValue = ((DefaultSettingValueAttribute)a).Value;                     /* XXX this is a string.. do we convert? */
                    // note: for StringCollection, TypeDescriptor.GetConverter(prop.PropertyType) returns
                    // CollectionConverter, however this class cannot handle the XML serialized strings
                    if (prop.PropertyType == typeof(StringCollection))
                    {
                        XmlSerializer    xs     = new XmlSerializer(typeof(string[]));
                        string[]         values = (string[])xs.Deserialize(new StringReader((string)defaultValue));
                        StringCollection sc     = new StringCollection();
                        sc.AddRange(values);
                        defaultValue = sc;
                    }
                    else if (prop.PropertyType != typeof(string))
                    {
                        defaultValue = TypeDescriptor.GetConverter(prop.PropertyType).ConvertFromString((string)defaultValue);
                    }
                }
                else if (a is SettingsSerializeAsAttribute)
                {
                    serializeAs = ((SettingsSerializeAsAttribute)a).SerializeAs;
                }
                else if (a is ApplicationScopedSettingAttribute ||
                         a is UserScopedSettingAttribute)
                {
                    dict.Add(a.GetType(), a);
                }
                else
                {
                    dict.Add(a.GetType(), a);
                }
            }

            SettingsProperty setting =
                new SettingsProperty(prop.Name, prop.PropertyType, provider, false /* XXX */,
                                     defaultValue /* XXX always a string? */, serializeAs, dict,
                                     false, false);


            if (providerService != null)
            {
                setting.Provider = providerService.GetSettingsProvider(setting);
            }

            if (provider == null)
            {
                if (local_provider == null)
                {
                    local_provider = new LocalFileSettingsProvider();
                    local_provider.Initialize(null, null);
                }
                setting.Provider = local_provider;
                // .NET ends up to set this to providers.
                provider = local_provider;
            }

            if (provider != null)
            {
                /* make sure we're using the same instance of a
                 * given provider across multiple properties */
                SettingsProvider p = Providers[provider.Name];
                if (p != null)
                {
                    setting.Provider = p;
                }
            }

            properties.Add(setting);

            if (setting.Provider != null && Providers [setting.Provider.Name] == null)
            {
                Providers.Add(setting.Provider);
            }
        }