Visual Studio Settings.settings 文件
有没有办法在运行时在 Settings.settings 文件中创建新设置?
例如,我想从一个类函数写入设置文件,并从另一个类函数读取该值。不,我不想传递这些价值观。
- 我知道如何从 Settings.settings 文件中获取值 (value = Properties.Settings.Default.XXX)
- 我知道如何更新现有值 (Properties.Settings.Default.XXX = newValue; Properties.Settings.Default.Save())
我想知道如何将“名称”、“类型”、“范围”和“值”添加到 Settings.settings 文件中在运行时。
任何建议将不胜感激!
谢谢, 伊瓦尔
Is there a way of creating a new setting in the Settings.settings file during run time?
For example, I want to write to the settings file from one class function, and read that value from a different class function. And no, I don't want to pass the values.
- I know how to get values from the Settings.settings file
(value = Properties.Settings.Default.XXX) - I know how to update an existing value
(Properties.Settings.Default.XXX = newValue; Properties.Settings.Default.Save())
I want to know how I can add "Name", "Type", "Scope" and "Value" into the Settings.settings file during run time.
Any suggestions would be appreciated!
Thanks,
Ivar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题
我相信 Visual Studio 在设计应用程序设置和值时会生成代码,因此在运行时这并不容易,在最坏的情况下,如果没有设计人员,这是不可能的。然而,有时您可以在运行时调用设计功能。
您会注意到隐藏代码具有您在设计器中创建的 C# 属性。例如,我添加了一个设置:
代码隐藏已生成:(
代码生成是您拥有强类型设置的原因)
我不确定您是否可以在运行时实现同样的效果。您必须生成代码并将其动态反馈给 JIT 编译器或类似的东西。或者也许还有另一种我不知道的方式,以我对设置的有限理解。
建议/解决方法
我建议找出一种替代/更简单的方法,而不是跳过障碍。例如,设置一个可序列化的集合类型,以便它可以存储多个值。
例如,您可以在一种设置下存储多个年龄:
您最终可能会使用 C# 代码来管理它,如下所示:
The Issues
I believe Visual Studio generates code when you design the application settings and values, therefore at runtime this would not be easy and at worst impossible without a designer. However you can sometimes call upon design features at runtime.
You'll notice the code-behind has the properties in C# that you create in your designer. For example, I added a setting for:
The code-behind has generated:
(The code generation is why you have strongly-typed settings)
I'm unsure if you could effect this same thing at runtime. You would have to generate code and feed it back to the JIT compiler dynamically or something like that. Or maybe there's another way I don't know about in my limited understanding of settings.
Suggestion/Workaround
I'd suggest figuring out an alternate/easier way instead of jumping through hoops. For example, make one setting a collection type that is serializable so it can store multiple values.
Then you can, for example, store multiple ages under just one setting:
You might end up with C# code to manage it like: