应用程序设置,无需保存到 app.config
Windows 窗体中的应用程序设置是存储应用程序范围属性的便捷方法。然而,在我当前的项目中,这些设置充斥着或多或少静态的颜色定义,导致 app.config 中出现噪音。有没有什么好方法可以防止设置被写入 app.config,仅依赖默认值?
我尝试将有问题的设置作为资源移出,但据我所知,Visual Studio 中的 Windows 窗体设计器没有提供将颜色资源值分配给控件上的属性的方法。
Application settings in Windows Forms is a convenient way to store application-wide properties. However, on my current project, these settings are littered with color definitions which are more or less static, causing noise in app.config. Are there any good way of preventing settings from being written to app.config, simply relying on the default value?
I tried moving the settings in question out as resources, but as far as I can tell, the Windows Forms designer in Visual studio provides no means of assigning color resource values to properties on controls.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
应用程序设置是存放所有这些信息的正确位置。如果您担心噪音,请构建一个具有属性的类来保存您要保存的每个颜色属性。使用各种 [Serializing] 属性标记该类您可以将其放置在应用程序设置中,就像其他任何东西一样。现在,您的“嘈杂”颜色设置嵌套在层次结构中,不会混乱或淹没其他更重要的设置。
Application settings is the right place for all of this information. If you're worried about noise, build a class with properties to hold each of the color attributes you are saving. Mark the class up with the various [Serializable] attributes and you can place it in Application settings just like anything else. Now your "noisy" color settings are nested in the hierarchy and won't clutter up or drown out other, more significant, settings.
您可以使用设置,它已经准备好了特别是对于用户/机器特定配置。您可以存储简单的类型或更复杂的类型,但随后您必须(通常)将它们序列化为 XML 并存储为字符串。
You can use Settings, it's prepared especially for user/machine specific configuration. You can store there simple types or more complicated but then you'll have to (usually) serialize them to XML and store as string.
您是否有权访问可以存储键值对的数据库?或者,或者如果它们是暂时的,那么您可以将它们存储在内存中的字典对象中。
存储在资源文件中听起来也是个好主意。您可以编写一个例程来迭代一组键值对。
Do you have access to a database you could store key value pairs in? Either that, or if they can be transient then you could store them in a dictionary object in memory.
Storing in a resource file sounds like a good idea too. You could just write a routine to iterate through a set of key value pairs.