最佳实践:我应该如何存储设置在C#(格式/类型)? [英] Best-Practices : how should I store settings in C# (Format/Type)?
问题描述
现在我很好奇什么是存储/加载设置在.net的可能性。例如我需要存储用户名/密码为不同的数据库和etc ..,还需要存储一些选项等。
Now I'm curious what are possibilities to store/load settings in .net. For example I need to store user name/password for different db's and etc.., also I need to store some options and etc.
我的想法是创建[Seriazable ]类并将其保存到文件...
My though was to create [Seriazable] class and save it to file...
您可以建议什么?
推荐答案
如果要存储应用程序配置设置您可以利用App.Config或Web.Config文件中的AppSettings,具体取决于它是否为Windows或Web应用程序。
我相信读取配置值的语法是
If you want to store application configuration settings, you can leverage the 'AppSettings' in the App.Config or Web.Config file depending on if its a windows or web application. I believe the syntax for reading the configuration values is
string configValue = Configuration.AppSettings["ConfigValueName"];
配置文件将如下所示:
<configuration>
<appSettings>
<add key="ConfigValueName" value="ABC"/>
</appSettings>
</configuration>
可能还有很多其他的东西。
With probably lots of other stuff.
如果您需要存储系统中有关用户信息或其他重复实体的信息,您需要构建一个数据库,并编写代码以保存/读取数据。
If you need to store information about users or other repeated entities in your system, you will need to build a database and write code to persist / read data to / from the database.
您可以使一个类可序列化,并自动序列化为XML或二进制,或者您可以使用SQL数据库。有许多.net技术用于访问数据库,只需查找ADO.net,LINQ和Entity Framework。
You could make a class serializable and automatically serialize it to XML or Binary, OR you could use a SQL database. There are many .net technologies for accesing databases just look up ADO.net , LINQ and the Entity Framework.
这篇关于最佳实践:我应该如何存储设置在C#(格式/类型)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!