c# - 在 Settings.settings 中保存属性

标签 c# .net visual-studio-2010 properties settings

我正在尝试在运行时保存一些数据:

SettingsProperty property = new SettingsProperty("tabSetting");
property.Name = "tab";
property.PropertyType=typeof(string);
property.DefaultValue = "path";
Settings.Default.Properties.Add(property);
Settings.Default.Save();

但此代码不会保存文件 Settings.settings 中的任何更改。

我还尝试从 VisualStudio 中更改 Settings.settings,我打开此文件并手动写入设置。之后,我运行上面提到的代码并得到此异常:

Item has already been added. Key in dictionary: 'tab'  Key being added: 'tab'

为什么Settings.Default.Save()代码不起作用?

最佳答案

您没有说明您的设置是用户范围内的还是应用程序范围内的。设置文件的保存位置会有所不同。听起来好像您的设置是用户范围,并且您正在应用程序设置文件中查找它们。异常是设置已经存在的线索。查看此问题/答案以获取更多信息。 When using a Settings.settings file in .NET, where is the config actually stored?

关于c# - 在 Settings.settings 中保存属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13170535/

相关文章:

.net - 如何抑制此 COM 泛型警告?

asp.net - 如何将 2 个日历彼此相邻而不是彼此放置?

linq - 升级到 4.0 : The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference? )

c# - 如何将 'state' 查询参数添加到 OAuth 请求(或忽略它)

c# - 有没有一种方法可以在 Switch 语句中执行某些代码,该语句仅在通过任何案例时才执行?

c# - 如何更好地使用 KeyDown 事件验证?

c# - 他们是在调整大小时阻止图片框闪烁的方法吗?

c# - 是否可以重载或更改复制和粘贴功能?

.net - .NET反射-从实例属性获取声明类的类型

c# - 使用 Javascript 过滤 GridView