/// <summary> /// Loads the specified options file. /// </summary> /// <param name="optionsFile">The options file.</param> /// <returns>The deserialized instance of this class</returns> public static MainOptions Load(string optionsFile) { MainOptions options = null; //the instance to return if (File.Exists(optionsFile)) { //Initializes a new instance of the class, to deserialize the options file XmlSerializer serializer = new XmlSerializer(typeof(MainOptions)); //Read option files StreamReader streamReader = new StreamReader(optionsFile); XmlTextReader xmlReader = new XmlTextReader(streamReader); //Deserializes it if (serializer.CanDeserialize(xmlReader)) { options = (MainOptions)serializer.Deserialize(xmlReader); } //Closes rssources xmlReader.Close(); streamReader.Close(); } //If the file doesn't exist or contains error, create a new one. if (options == null) { options = CreateNewOptionsFile(optionsFile); } return(options); }
/// <summary> /// Loads the options. /// </summary> /// <returns></returns> private static bool LoadOptions() { try { //Deserializes options file as MainOptions class into Option Options = MainOptions.Load(); return(true); } catch (Exception ex) { //Houston, we've a problem. if (DialogResult.No == MessageBox.Show("Error loading preferences file.\n" + ex.Message + "\n\nDo you want to create a new file ?\n\nIf yes, your previous dots will be lost.\nIf no, SeaChart will terminate and you can manually fix the options file.", "Preferences error", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)) { //The user wants to terminate SeaChart to manually fix the problem. return(false); } } //The uesrs wants to create a new options file try { Options = MainOptions.CreateNewOptionsFile(MainOptions.DefaultOptionsFile); return(true); } catch (Exception ex) { //Arg... a very bad day for the user. MessageBox.Show(ex.Message, "Preferences error", MessageBoxButtons.OK, MessageBoxIcon.Hand); return(false); } }
/// <summary> /// Creates a new options file. /// </summary> /// <param name="optionsFile">The options file path.</param> /// <returns>A new instance of this class</returns> public static MainOptions CreateNewOptionsFile(string optionsFile) { //Gets the directory, and create it if it doesn't exist yet string directoryName = Path.GetDirectoryName(DefaultOptionsFile); if (directoryName != "" && !Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } //Returns a new instance of the class, and serializes it into the specified XML file. MainOptions options = new MainOptions(); options.Save(optionsFile); return(options); }