user271077
user271077

Reputation: 1006

Can I save an object to the app.config file?

I need to use a config file for my application which will save a list of details for each entity: for example-

<person A>
         <name= >
         <age= >
<person B>
         <name=>
         <age= >

is something of this sort possile in the settings class or this requires using the app.config file wihtout the settings class wrapper?

Edit: My application used to have a single entity, now it should support multiple entities and therefore save multiple entities in some config.

Upvotes: 3

Views: 4121

Answers (3)

Doobi
Doobi

Reputation: 4842

The config file is not a good place to store dynamic configuration information, it's intended for configuring an app, not maintaining state.

Rather use a separate file, or something like a SQLite database to store dynamic data.

Upvotes: 1

Smur
Smur

Reputation: 3113

Well, you could use Serialization for this purpose. But I still don't see the point of storing it in the .config file. Anyway, you could serialize the object in a XML format and store it in a key that you would add to the .config file, as a string. I guess that would solve it.

If you decide to store it as a simple XML file, use the XMLDocument and XMLNode classes.

Upvotes: 1

delete
delete

Reputation:

Yes, you can save any sort of information (strings) in .config file. You'd then use the ConfigurationManager class to access the saved information by using a Key.

But why would you want to save information like this in the .config?

I recommend you use an XML document for this purpose. You can then use the XDocument class to parse it.

Edit:

After reading your comment, I think the app.config file cannot save an object. So you could not save a class object there.

Upvotes: 1

Related Questions