Question

I am using C# ,win forms and .net 2.0

I am trying to add a property inside user settings file at run time but i am not able to view that added property in settings.settings file in certain location i.e. file exists but property is not added

I am not getting error when i call this property it works Using this below code

MessageBox.Show(***********.Properties.Settings.Default.Properties["NewProperty"].DefaultValue);

I have written this following code Calling the function

clCommonFuncation cl = new clCommonFuncation();
        if (***********.Properties.Settings.Default.Properties["NewProperty"] == null)
        {
            cl.addPropertyinSettingsFile("NewProperty",
            ***********.Properties.Settings.Default.Providers,
            ***********.Properties.Settings.Default.Providers["LocalFileSettingsProvider"],
            ***********.Properties.Settings.Default.Properties,
            typeof(string),"ASD",null);
            ***********.Properties.Settings.Default.Save();
            ***********.Properties.Settings.Default.Reload();
        }

And this is calling funaction

 public void addPropertyinSettingsFile(string settingName,
        SettingsProviderCollection settingsProviderCollection,
        SettingsProvider settingsProvider,
        SettingsPropertyCollection settingPrpertyCollection,
        Type dataType,
        object defaultValue,
        object settingDefault)
    {
        SettingsProperty lvSettingProperty = new SettingsProperty(settingName);
        lvSettingProperty.DefaultValue = defaultValue;
        lvSettingProperty.IsReadOnly = false;
        lvSettingProperty.PropertyType = dataType;
        lvSettingProperty.Provider = settingsProvider;
        lvSettingProperty.SerializeAs = SettingsSerializeAs.String;
        lvSettingProperty.Name = settingName;
        lvSettingProperty.Attributes.Add(typeof(System.Configuration.UserScopedSettingAttribute),
            new System.Configuration.UserScopedSettingAttribute());
        settingPrpertyCollection.Add(lvSettingProperty);            
    }

What is it i am doing wrong? Any suggestion will be appreciated Thank you

Was it helpful?

Solution

I think you'd better write a custom struct or class with your application settings and use serialization for loading and saving it - that is much more clear and relevant in your case.

OTHER TIPS

You can not add or remove properties in .Net Setting files at runtime. There are some tricks round the web but none of them is applicable and a solution to what you want.

Settings files have not been designed for such a purpose. These files have been designed to be populated at design time and only be "read" or "modified" during runtime.

The reason is that when you create and edit settings file in designer (by double clicking on a settings file in solution explorer or choosing settings tab at project properties menu item) Visual studio creates an ApplicationSettings class (Derived from ApplicationSettingsBase class) which has data members for any setting field you created plus some additional attributes (like [ApplicationScopedSetting] or [UserScopedSetings] ). At run time the .Net runtime interacts with seetings files using this class. Therefore when you try to add properties at run time, you have no backing fileds and attributes in the ApplicationSettings class and CLR does not know what to do with them.

Conclusion: The Settings file have specific usage and are not suitable for any arbitrary configuration persistence in you application. Try using XML files which fully support what you want from reading, writing, adding, removing, updating basic types properties (string, char, int and so on) to supporting complex objects using XML.Seriliazation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top