public void Test() { // create settings for testing MySettings settingsTest = new MySettings(); // save settings to file. You could also pass a path created from a SaveFileDialog, or sth. similar. settingsTest.Save(MySettings.GetDefaultPath()); // Load settings. You could also pass a path created from an OpenFileDialog. MySettings anotherTest = MySettings.Load(MySettings.GetDefaultPath()); // do stuff with the settings. }
public static MySettings Load(string path) { if (!System.IO.File.Exists(path)) { throw new System.ArgumentException("File \"" + path + "\" does not exist."); } try { MySettings result = null; SharpSerializerXmlSettings settings = new SharpSerializerXmlSettings(); SharpSerializer serializer = new SharpSerializer(settings); result = (MySettings)serializer.Deserialize(path); return(result); } catch (Exception err) { throw new InvalidOperationException(string.Format("Error in MySettings.LoadConfiguration():\r\nMessage:\r\n{0}\r\nStackTrace:\r\n{1}", err.Message, err.StackTrace), err); } }
// public static MySettings Load(string path) { if (!System.IO.File.Exists(path)) throw new System.ArgumentException("File \"" + path + "\" does not exist."); try { MySettings result = null; // the serialization settings are just a needed standard object as long as you don't want to do something special. SharpSerializerXmlSettings settings = new SharpSerializerXmlSettings(); // create the serializer. SharpSerializer serializer = new SharpSerializer(settings); // deserialize from File and receive an object containing our deserialized settings, that means: a MySettings Object with every public property in the state that they were saved in. result = (MySettings)serializer.Deserialize(path); // return deserialized settings. return result; } catch (Exception err) { throw new InvalidOperationException(string.Format("Error in MySettings.LoadConfiguration():\r\nMessage:\r\n{0}\r\nStackTrace:\r\n{1}", err.Message, err.StackTrace), err); } }