/// <summary> /// Saves all settings found in the singleton instance of SettingsManager to the default filename in the execution directory. /// </summary> /// <returns>True if successfully saved, false if not</returns> public bool SaveSettingsManager() { XmlDocument parentXmlDoc = new XmlDocument(); // XML Declaration parentXmlDoc.AppendChild(parentXmlDoc.CreateNode(XmlNodeType.XmlDeclaration, string.Empty, string.Empty)); XmlElement xmlElem = parentXmlDoc.CreateElement(string.Empty, SettingsManagerString, string.Empty); xmlElem.AppendChild(XmlIOHelpers.CreateKeyValueXmlElement(OpenFileDirectoryPropertyName, this.OpenFileDirectory, parentXmlDoc)); xmlElem.AppendChild(XmlIOHelpers.CreateKeyValueXmlElement(SaveFileDirectoryPropertyName, this.SaveFileDirectory, parentXmlDoc)); // SetsExcludedFromSearches XmlElement setsExcludedFromSearches = parentXmlDoc.CreateElement(string.Empty, SettingsManager.SetsExcludedFromSearchesPropertyName, string.Empty); foreach (Guid setGuid in this.SetsExcludedFromSearches) { setsExcludedFromSearches.AppendChild(XmlIOHelpers.CreateKeyValueXmlElement(SetString, setGuid, parentXmlDoc)); } xmlElem.AppendChild(setsExcludedFromSearches); parentXmlDoc.AppendChild(xmlElem); try { parentXmlDoc.Save(this._FullPathName); } catch (Exception e) { Console.WriteLine(e.Message); return(false); } return(true); }
/// <summary> /// Reads the Settings file and sets all the values of the singleton-instance of SettingsManager accordingly. If the /// settings file doesn't exist, it simply leaves everything at default. /// </summary> private void ImportSettings() { // If the settings file doesn't exist, then don't try to import it, just leave and use the defaults. if (!System.IO.File.Exists(this._FullPathName)) { return; } XmlTextReader reader = new XmlTextReader(this._FullPathName); reader.WhitespaceHandling = WhitespaceHandling.None; XmlDocument xd = new XmlDocument(); xd.Load(reader); XmlNode xnodDE = xd.DocumentElement; if (xnodDE.Name != SettingsManagerString) { throw new InvalidOperationException("Root node is not " + SettingsManagerString); } var childNodes = XmlIOHelpers.GetChildNodesWithMatchingNames ( xnodDE, new List <string>() { OpenFileDirectoryPropertyName, SaveFileDirectoryPropertyName, SetsExcludedFromSearchesPropertyName, } ); if (childNodes.ContainsKey(OpenFileDirectoryPropertyName)) { this.OpenFileDirectory = childNodes[OpenFileDirectoryPropertyName].InnerText; } if (childNodes.ContainsKey(SaveFileDirectoryPropertyName)) { this.SaveFileDirectory = childNodes[SaveFileDirectoryPropertyName].InnerText; } if (childNodes.ContainsKey(SetsExcludedFromSearchesPropertyName)) { this.SetsExcludedFromSearches.AddRange ( SettingsManager.CreateSetsExcludedFromSearchesListFromXmlElement(childNodes[SetsExcludedFromSearchesPropertyName]) ); } }