/// <summary> /// Saves the current settings to the .ini file /// </summary> public void SaveSettings() { // Start by organizing the values into a tree SettingsNode baseNode = new SettingsNode(); foreach (string value in _values.Keys) { string path = value; // Base settings, create values at the base node if (path.IndexOf("\\", StringComparison.Ordinal) == -1) { baseNode[value] = _values[value]; } else { string[] subPath = path.Split('\\'); path = ""; for (int i = 0; i < subPath.Length - 1; i++) { if (path == "") { path += subPath[i]; } else { path += "\\" + subPath[i]; } } baseNode.CreateNode(path)[subPath[subPath.Length - 1]] = _values[value]; } } // Sort the nodes after they are created baseNode.Sort(); // Mount the settings string StringBuilder output = new StringBuilder(); baseNode.SaveToString(output); using (FileStream stream = new FileStream(_filePath, FileMode.OpenOrCreate, FileAccess.Write)) { stream.SetLength(0); // Save the settings to the settings file now StreamWriter writer = new StreamWriter(stream, Encoding.UTF8); writer.Write(output.ToString().Trim()); writer.Close(); writer.Dispose(); } baseNode.Clear(); }
/// <summary> /// Creates and returns the given node path /// If the node path already exists, it is not re-created /// </summary> /// <param name="path">The path to the node, separated by '\'</param> /// <returns>The topmost node of the node list</returns> public SettingsNode CreateNode(string path) { if (path == "") { return(this); } SettingsNode returnNode = null; string[] nodes = new string[1]; if (path.IndexOf("\\", StringComparison.Ordinal) != -1) { nodes = path.Split('\\'); } else { nodes[0] = path; } foreach (SettingsNode node in _subNodes) { if (node._name == nodes[0]) { if (nodes.Length == 0) { returnNode = node; } else { string nextPath = ""; for (int i = 1; i < nodes.Length; i++) { if (nextPath == "") { nextPath += nodes[i]; } else { nextPath += "\\" + nodes[i]; } } returnNode = node.CreateNode(nextPath); } break; } } // If no node was found, create a new one if (returnNode == null) { string nextPath = ""; for (int i = 1; i < nodes.Length; i++) { if (nextPath == "") { nextPath += nodes[i]; } else { nextPath += "\\" + nodes[i]; } } SettingsNode newNode = new SettingsNode { _parentNode = this, _name = nodes[0] }; _subNodes.Add(newNode); returnNode = nextPath == "" ? newNode : newNode.CreateNode(nextPath); } return(returnNode); }