Reputation: 2000
This is the code i am using to add a new setting to the settings.settings file but it is not working.
System.Configuration.SettingsProperty property = new System.Configuration.SettingsProperty("CustomSetting");
property.DefaultValue = "Default";
property.IsReadOnly = false;
property.PropertyType = typeof(string);
property.Provider = Properties.Settings.Default.Providers["LocalFileSettingsProvider"];
property.Attributes.Add(typeof(System.Configuration.UserScopedSettingAttribute), new System.Configuration.UserScopedSettingAttribute());
Properties.Settings.Default.Properties.Add(property);
// Load settings now.
Properties.Settings.Default.Reload();
// Update the user itnerface.
Properties.Settings.Default["CustomSetting"] = txt_Cipher.Text;
Properties.Settings.Default.Save();
txt_Cipher.Text = string.Empty;
The new setting is not being added in the settings file. This is the new code i have been trying according to sumesh's reply.
System.Configuration.SettingsProperty property = new System.Configuration.SettingsProperty("CI");
Properties.Settings.Default["CI"] = txt_Cipher.Text;
Properties.Settings.Default.Save();
Properties.Settings.Default.Reload();
txt_Cipher.Text = string.Empty;
I have already created a setting in the settings.setting file as said in the Image by sumesh.
Upvotes: 2
Views: 10921
Reputation: 1988
Add CustomSetting manually in your settings.settings file
System.Configuration.SettingsProperty property = new System.Configuration.SettingsProperty("CustomSetting");
Properties.Settings.Default["CustomSetting"] = txt_Cipher.Text;
Properties.Settings.Default.Save();
txt_Cipher.Text = string.Empty;
use this code to update the settings
its working fine for me and an xml file is created for this settings as
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<userSettings>
<WindowsFormsApplication1.Properties.Settings>
<setting name="CustomSetting" serializeAs="String">
<value>1</value>
</setting>
</WindowsFormsApplication1.Properties.Settings>
</userSettings>
</configuration>
Please add customsSettings as shown in the picture
Upvotes: 2