问题
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.
My though was to create [Seriazable] class and save it to file...
What can You suggest? what are possibilities in .net and etc.
回答1:
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"];
The configuration file will look like this
<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.
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.
回答2:
The .NET Framework provides some mechanisms for storing and loading application and/or user settings. See "MSDN: Using Settings in C#" for the basics.
To encrypt some sensible data within your configuration files you can also use some standard functions of the .NET Framework. For a short introduction take a look at "Encrypting Configuration Information Using Protected Configuration" and "Encrypting Passwords in a .NET app.config File".
回答3:
Well the most common way of storing and loading settings is to use some kind of XML data, and making a [Serializable] class and outputting the serialized class to an XML file would work, but you have to keep the following things in mind:
- You won't have any control over the input data -- because the XML file can be edited outside of your application, it's possible to have completely nonsensical data loaded
- You'll want to have sane defaults for when nodes are missing from the XML. This can be caused by manual edits, or loading an old version of your class into a newer version.
- I would personally be very hesitant to store any passwords in a configuration file anywhere, without using some kind of asymmetrical encryption algorithm. If I found a program that was storing my passwords in plain-text in an XML file, I would stop using that program.
回答4:
You can use the resource files for less sensitive info. For usernames/passwords you need to use an ecrypted text file, or make use of CSPs.
回答5:
I recommend store where you need to App.Config, an Ini file... Then use the library Config.Net: A comprehensive easy to use and powerful .NET configuration library, fully covered with unit tests and tested in the wild on thousands of servers and applications. Found it in GitHub: https://github.com/aloneguid/config
So you can have your settings in a place, and later, if you need to, move to another.
来源:https://stackoverflow.com/questions/535685/best-practices-how-should-i-store-settings-in-c-sharp-format-type