开发者

Writing custom sections into app.config

I want to save some custom data into application configuration file and I need to create some custom sections in app.config. Reading custom data from app.config is simple task, but I can't write information from my programm into app.config. For finding solution of this problem I create test project.

For reading data from custom section app.config I used开发者_开发知识库 information from this article:

http://devlicio.us/blogs/derik_whittaker/archive/2006/11/13/app-config-and-custom-configuration-sections.aspx


You really ought not to write anything to app.config, because if you do then you are limiting use of your app to Administrators only. It's better practice to write settings to a separate .config file located in a user profile folder, e.g. <profiles>\<user name>\Application Data\<your product name>.


First override IsReadyOnly() in your CustomConfigSection to return false.

Once you've done that you should be able to do something like this (this is for ASP.NET, but it might be transferably):

System.Configuration.Configuration configFile = WebConfigurationManager.OpenWebConfiguration("~");
CustomConfigSection config = (CustomConfigSection)configFile.GetSection("Custom");
config.Tweak = 1;
config.Change = "foo";
configFile.Save();

Give that a try.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜