How to save/read setting options?

goodmorningsky

Well-known member
Joined
Aug 18, 2003
Messages
172
My app contains options that user can set and the options are applied to app when it is launced.

What is standard way to do this?
My guess are using App configuration xml file, using Window registry or etc.

If you have any code example please post it.
Thank you all.
 
I think the best way would be for you to use Xml Serialization to write an xml file with all your settings. Heres an example:

Make a class with all your options in it:

public class Options
{
public string opt1;
public int opt2;
public string opt3;
}

Then create some functions that will serialize and deserialize the info:

public static void Serialize(Options Prefs)
{
//create xml serializer
XmlSerializer Serializer = new XmlSerializer(typeof(Options));
//create writer
StreamWriter Writer = new StreamWriter(Path);
//serialize Data in the options class with the writer
Serializer.Serialize(Writer, Prefs);
//close the writer
Writer.Close();
}



public static Options Deserialize()
{
//create xml serializer
XmlSerializer Serializer = new XmlSerializer(typeof(Options));
//create reader
StreamReader Reader = new StreamReader(Path);
//deserialize the xml and load it into the Prefs variable
Options Prefs = (Options)Serializer.Deserialize(Reader);
//close the reader
Reader.Close();
//return the Options
return Prefs;
}

Tell me if you need me to explain a little better. :)


Dan
 
Hi I have a question one this serialization example. Could the above sample be easily modified to handle multiple objects that have been serialized? I know conceptually it dosnt work in this case, as there would only ever be one "options" object per project, but for the sake of the exercise could this be done. The serialization isde is ok, I guess youd just dupe the "Serializer.Serialize(Writer, Prefs);" line per object. What about when deserializing? How do you specify the object to deserialize?

Thanks a heap! SEVI
 
Updated Example

Im sorry, the link I posted was incorrect. Now I cant seem to find the proper article.

However, I still have the source download from the article, which also includes a copy of it in a Word document. I have attached it here.
 

Attachments

Back
Top