>Calling save would cause an exception Viv but aside from that I will have to think of something else to achieve what I'm after, I just came across this code on code project and thought I'd give it a shot, it's OK but very rigid in its design , what I'm after is a file ( doesn't have to be app..config ) containing values that are grouped in sections ( like the old INI files ) I suppose I can use plain old XML files or indeed INI files.
If you use the build in configuration manager you can define your own custom sections - but it's pretty rigid (and *.config files aren't necessarily the best place).
Or, as you mention, you could use standalone XML files using the System.Xml.Serialization classes.
A third option is storing as json objects. I like this one because it's compact and can easily handle high levels of complexity. Also, if your using it for a web application there's always the option of passing the json directly to the browser.
The NewtonSoft serializer is ideal for this. Crude example - given the following classes:
public class Destinations
{
public List<Data> data = new List<Data>();
}
public class Data
{
public string Destination { get; set; }
public string DialCode { get; set; }
}
you could:
string path = @"D:\tmp3\myconfig.json";
Destinations ds = new Destinations();
ds.data.Add(new Data { Destination = "Hereford", DialCode = "91432" });
ds.data.Add(new Data { Destination = "Hay", DialCode = "01497" });
var j = JsonConvert.SerializeObject(ds);
System.IO.File.WriteAllText(path,j);
var j2 = System.IO.File.ReadAllText(path);
Destinations ds2 = JsonConvert.DeserializeObject<Destinations>(j2);
ds2.data.Add(new Data{Destination = "Monmouth",DialCode = "0600"});
System.IO.File.WriteAllText(path,JsonConvert.SerializeObject(ds2));
There's a lot more stuff in the NewtonSoft package including streaming and even a 'Linq to Json'. It's available as a NuGet package....