/// <summary> /// Loads the specified options file. /// </summary> /// <param name="optionsFile">The options file.</param> /// <returns>Instance of this options class based on options files</returns> static public MainOptions Load(string optionsFile) { if (!File.Exists(optionsFile)) { return(CreateNewOptionsFile(optionsFile)); } // Create an XmlSerializer to use for retrieving options values XmlSerializer mySerializer = new XmlSerializer(typeof(MainOptions)); // Create a StreamReader to point to the options file StreamReader myTextReader = new StreamReader(optionsFile); // Create an XmlTextReader to actually read the options. XmlTextReader myXmlReader = new XmlTextReader(myTextReader); if (mySerializer.CanDeserialize(myXmlReader)) { MainOptions options = ((MainOptions)mySerializer.Deserialize(myXmlReader)); // Close the IO objects we've used. myXmlReader.Close(); myTextReader.Close(); // Return deserialized result return(options); } else { // Close the IO objects we've used. myXmlReader.Close(); myTextReader.Close(); return(CreateNewOptionsFile(optionsFile)); } }
/// <summary> /// Creates a new options file. /// </summary> /// <param name="optionsFile">The filename.</param> static public MainOptions CreateNewOptionsFile(string optionsFile) { //Le dossier existe-t-il ? string folder = Path.GetDirectoryName(MainOptions.DefaultOptionsFile); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } //Créons un nouveau fichier //contenant les préférences par défaut //et enregistrons-le MainOptions options = new MainOptions(); options.SetDefaults(); options.Save(optionsFile); return(options); }
/// <summary> /// Creates a new options file. /// </summary> /// <param name="optionsFile">The filename.</param> static public MainOptions CreateNewOptionsFile (string optionsFile) { //Le dossier existe-t-il ? string folder = Path.GetDirectoryName(MainOptions.DefaultOptionsFile); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } //Créons un nouveau fichier //contenant les préférences par défaut //et enregistrons-le MainOptions options = new MainOptions(); options.SetDefaults(); options.Save(optionsFile); return options; }