这是我用来向settings.settings文件添加新设置的代码,但它不起作用。在Settings.setting文件中添加一个新设置
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;
新设置未被添加到设置文件中。根据sumesh的回复,这是我一直在尝试的新代码。
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;
我已经在settings.setting文件中创建了一个设置,如通过sumesh在图像中所述。
该代码是否会抛出任何异常? – Sumeshk
不,它不,但我不能看到设置文件中的任何更改 – wintersolider
我认为没有添加CustomSetting,因为一旦它被添加Properties.Settings.Default.Properties.Add(property);会抛出键已经存在的错误。 – Sumeshk