Multiple settings files in .NET

和自甴很熟 提交于 2019-12-23 02:24:08

问题


I'm currently using My.Settings/Properties.Settings to store various user settings. Many of these settings are bound to controls in various Forms.

However I want a greater degree of control - specifically I would like to be able reload or save specific groups of settings. Using the in-built Settings.settings file, I can only handle these settings as a single group.

I know that I can create additional settings files, and this article (Summary section) suggests that if Settings.settings is not present, the application will look for other .settings files in the project root, but this doesn't seem to be the case for me (unless I am doing something wrong):

They offer tremendous power and flexibility. For example, you can add customized settings files to your project for management of settings in different groups. This allows you to save settings for a group or subset of values in separate files which can save saving and loading time.

I'm on the point of building my own Settings class and doing it all myself, but it would be a shame - the in-built functionality suits me well - providing I can have multiple settings files.

UPDATE: This article suggests that you can add additional settings files and drag them across to the Properties folder. Unfortunately, my current project is in VB.Net and this behaviour does not appear to be replicated.


回答1:


The solution, it appears, is to add as many additional .settings files as needed. In the properties for each, add in a Customer Tool Name - in my case I chose 'My' to match the in-built Setting.settings.

Thereafter you can access each settings file independently, as follows:

My.Options1.Default.Foo = "bar"
My.Options2.Default.Foo = "bar"

My.Options1.Default.Save()
My.Options2.Default.Reload()

The .default is a minor annoyance, but other than that, this appears to work as I hoped.



来源:https://stackoverflow.com/questions/10014239/multiple-settings-files-in-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!