C#中的应用程序设置(更高级) [英] Aplication Settings in C# (more advanced)

查看:62
本文介绍了C#中的应用程序设置(更高级)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!



有些东西现在真的让我感觉不舒服。

我读了很多关于自定义配置部分和应用程序设置,但我找不到一篇好的文章/说明如何使用Settings.settings创建嵌套设置。

如果你添加Settings.settings文件到你的属性,如果你这样访问它将在代码中可用:

Hello!

There are some things that really grinds my gears right now.
I read lot about custom configuration sections and aplication settings, but I can NOT find one good article/explanation how to create nested settings using Settings.settings.
If you add Settings.settings file to your "Properties", it will be available in code if you access it like this:

Properties.Settings.Default.SomeExampleString;





如果我添加名为Settings2.settings的第二个文件,可以这样访问:



If I add second file, named Settings2.settings, it can be accessed like this:

Properties.Settings1.Default.SomeNextExampleString;





但如何让它看起来像:



But how to make it to look like:

Properties.Settings.Default.FirstString;
Properties.Settings.Advanced.SecondString;
Properties.Settings.Basic.ThirdString;





或者甚至可以在自动生成Settings.Designer.cs文件时?

我想使用Settings.settings,因为它实现了保存和重置方法。

任何想法?



Or it is even possible, when Settings.Designer.cs file is autogenerated?
I would like to use Settings.settings because of it's implemented save and reset methods.
Any ideas?

推荐答案

在我个人看来,应用程序设置在某种程度上已经过时了。相反,我使用了更强大且易于使用的武器:数据合同: http:// msdn。 microsoft.com/en-us/library/ms733127.aspx [ ^ ]。



它允许在流中存储任何对象图并将其恢复。该方法绝对是非侵入性的:您不修改数据类型,不遵循任何规则(没有访问修饰符,实现任何接口,没有),只需向类型和成员添加一些属性即成为合同的一部分。这不是任何设置的最佳机制吗?



并且,要将文件放在写入位置,请参阅:

http://msdn.microsoft.com/en-us/library/system.environment.getfolderpath.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.environment .specialfolder.aspx [ ^ ]。



这样,您可以使用每个用户帐户的应用程序数据,也可以使用所有用户或类似的用户数据。



-SA
In my personal opinion, application setting is somewhat morally obsolete. Instead, I use much more powerful and easy-to-use weapon: Data Contract: http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

It allows to store any object graph in stream and restore it back. The approach is absolutely non-intrusive: you don't modify data types, don't follow any rules (no access modifiers, implementing any interfaces, nothing), just add some attributes to types and members to became part of contract. Isn't it the best mechanism for any kinds of settings?

And, to put the file in write place, please see:
http://msdn.microsoft.com/en-us/library/system.environment.getfolderpath.aspx[^],
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx[^].

This way, you can use either application data per user account, or the one for "all users", or anything like that.

—SA


这篇关于C#中的应用程序设置(更高级)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆