예제 #1
0
        /// <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();
        }