【发布时间】:2014-05-19 04:10:00
【问题描述】:
这是我用来向 settings.settings 文件添加新设置的代码,但它不起作用。
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 图片中所述。
【问题讨论】:
-
代码是否抛出异常?
-
不,它没有,但我在设置文件中看不到任何更改
-
我认为没有添加自定义设置,因为一旦添加了 Properties.Settings.Default.Properties.Add(property);会抛出键已经存在的错误。
-
您是在 Settings.settings 中手动添加自定义设置吗?
-
我在设置文件中添加了一个名为newsetting的设置。那仍然存在,但除此之外没有添加新设置。我也没有任何例外。