views:

2463

answers:

5

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.

+3  A: 

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:

  1. 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
  2. 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.
  3. 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.
scwagner
Yeah, everything would be encrypted, that's for sure ;)
Lukas Šalkauskas
+1  A: 

You can use the Resource files for less sensitive info and for usernames/passwords, you need to use an ecrypted text file or make use of CSPs

[1]: http://en.wikipedia.org/wiki/Cryptographic_Service_Provider - 23k

Lonzo
+10  A: 

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".

Alexander
That last article, "Encrypting Passwords in..." fits the bill exactly for what I need. Thanks. +1
JMD
+1 For pointing the way to Jon Galloway's post.
magnifico
+2  A: 

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.

TJB
Yeah, this could be a possibility.
Lukas Šalkauskas
+1  A: 

I have asked a similar question here, maybe the answers help...

spinodal