.Net Persist application settings without using Properties.Settings
===

I've been reading for a while on how I can persist application settings over subsequent application loads. My application is a Winforms app.

I know there are several ways I can do this, and coming from a Delphi background I always used to use `.ini` files, but I can see XML is probably more flexible for my particular scenario.

I'm considering two possible solutions for my application to persist its settings. Note that I cannot use the Properties.Settings method for storing my data because of two reasons:

 - The data I need to persist is created dynamically, that is I may have 1,2,10 instances of my various classes at runtime (note that each one has its own "Name")
 - Some of these classes I instantiate are created from a class library (separate project) (which doesn't have a Settings section in my application)
 - I don't want to have to create 100's if not 1000's of settings in the Properties.Settings.

My two possible solutions are as follows:

 1. Create an abstract class to manage writing and retreiving settings by providing methods to `get` and `set` data, e.g. `GetBoolean("InstanceName","Setting", bool default)` and `SetBoolean("InstanceName","Setting")`. Each class that needs to be persisted can then implement this abstract class, so, when instantiated, reads its settings from the provided file and saves settings when specific properties change. 
 2. Mark my desired properties in each class as `Serializable`, and when instantiated, ensure the creating class object calls a `LoadSettings(string filename)` method on the class which loads the desired `Serialized` settings from the supplied file, and writes updates when `SaveSettings(string filename)` is called. I could be using an `IPersistent` interface for example, which my classes can implement.

I was hoping someone can point me to which method I should pursue.

Note that I need the ability to only persist certain properties in each instance of each class. I cannot see anyway of automating this, and will have to do this manually.

Note that these objects are instantiated at runtime, so I do knot know in advance what objects need to be persisted, only the specific properties within them. Consider the following example:

Lets say I have a `Serial` class (my own with some wrapper properties/methods etc around the `System.IO.SerialPort` class. The user, at runtime, may need 1,2,x number of serial ports available to them. I need some way to save the number of serial ports in use, and for each one, save their `Name`, `Portnumber`, `Baud`, `Parity` etc properties. 

So, given a XML file, I need to be able to read info that the application needs to create two serial ports, 1 called `GPS1` and the other called `GPS2`. For `GPS1` the `Portnumber` is `COM1` and for `GPS2` the `Portnumber` is `COM2` etc etc. 

Alas, the amount of data to be serialized is significant, this is only an example. I can envisage storing in the realm of tens to hundreds of objects(instances), each with ten or so settings.


----------


Some important features that I require:

The functionality to optionally let the user choose the settings file, so that I provide the ability to create settings "profiles" which the user can load and save to separate files. 
Each file can be moved from machine to machine. So, for example, if the user creates a profile on a PC, and then needs to change the PC out with another one, he/she can simply copy the file and load the same settings.

I typically only need to store primitive types (`string`, `int`, `float`, `boolean`,etc), although obviously the ability to store more complex types is an advantage.


Can anyone point me in the right direction? Or if my two solutions are not viable, suggest another?