C#窗口程序 XML配置文件的创建、使用、读取

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.IO;
using System.Drawing;

namespace MedicalLibrary
{
    class ConfigHelper
    {
        static string Direction = "Config";
        static string RealPath;

        /// <summary>
        /// 初始化文件和路径变量
        /// </summary>
        public static void Init(string file = "Settings")
        {
            string path = Helper.CombinePath(Direction);
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
                Console.WriteLine("创建配置文件夹" + path);
            }
            RealPath = Helper.CombinePath(Path.Combine(Direction, file + ".xml"));
            if (!File.Exists(RealPath))
            {
                FileStream fs = new FileStream(RealPath, FileMode.CreateNew);
                fs.Close();
                Console.WriteLine("创建配置文件" + RealPath);
            }
        }

        /// <summary>
        /// 写入默认设置到Settings.xml
        /// </summary>
        public static void WriteDefaultSetting()
        {
            Init();
            XDocument xml = new XDocument(
                new XDeclaration("1.0", "utf-8", "yes"),
                new XComment("设置"),
                new XElement("Settings",
                    new XComment("颜色设置"),
                    new XElement("Color",
                        new XComment("主色调"),
                        new XElement("Main",
                            new XAttribute("R", 203),
                            new XAttribute("G", 233),
                            new XAttribute("B", 207)),
                        new XComment("背景色"),
                        new XElement("Background",
                            new XAttribute("R", 255),
                            new XAttribute("G", 255),
                            new XAttribute("B", 255)),
                        new XComment("过渡色"),
                        new XElement("Transition",
                            new XAttribute("R", 223),
                            new XAttribute("G", 243),
                            new XAttribute("B", 227)),
                        new XComment("字体色"),
                        new XElement("Fore",
                            new XAttribute("R", 0),
                            new XAttribute("G", 0),
                            new XAttribute("B", 0)),
                        new XComment("按钮点击色"),
                        new XElement("ButtonClick",
                            new XAttribute("R", 192),
                            new XAttribute("G", 192),
                            new XAttribute("B", 192))
                    )
                )
            );
            xml.Save(RealPath);
        }

        /// <summary>
        /// 存储加载好的设置
        /// </summary>
        public class Settings
        {
            public static Color Color_Main;
            public static Color Color_Background;
            public static Color Color_Fore;
            public static Color Color_ButtonClick;
            public static Color Color_Transition;
        }

        /// <summary>
        /// 载入Settings.xml文件的信息到Settings类中
        /// </summary>
        public static void LoadSettings()
        {
            Init();
            XElement e = GetElement("Color", "Main");
            Settings.Color_Main = Color.FromArgb(
                int.Parse(e.Attribute("R").Value),
                int.Parse(e.Attribute("G").Value),
                int.Parse(e.Attribute("B").Value)
                );
            e = GetElement("Color", "Background");
            Settings.Color_Background = Color.FromArgb(
                int.Parse(e.Attribute("R").Value),
                int.Parse(e.Attribute("G").Value),
                int.Parse(e.Attribute("B").Value)
                );
            e = GetElement("Color", "Transition");
            Settings.Color_Transition = Color.FromArgb(
                int.Parse(e.Attribute("R").Value),
                int.Parse(e.Attribute("G").Value),
                int.Parse(e.Attribute("B").Value)
                );
            e = GetElement("Color", "Fore");
            Settings.Color_Fore = Color.FromArgb(
                int.Parse(e.Attribute("R").Value),
                int.Parse(e.Attribute("G").Value),
                int.Parse(e.Attribute("B").Value)
                );
            e = GetElement("Color", "ButtonClick");
            Settings.Color_ButtonClick = Color.FromArgb(
                int.Parse(e.Attribute("R").Value),
                int.Parse(e.Attribute("G").Value),
                int.Parse(e.Attribute("B").Value)
                );
        }

        public static XElement root = null;

        /// <summary>
        /// 通过链式string数组获取节点,空时返回根节点
        /// </summary>
        /// <param name="link"></param>
        /// <returns></returns>
        public static XElement GetElement(params string[] link)
        {
            Init();
            if (root == null)
            {
                root = XElement.Load(RealPath);
            }
            XElement xml = root;
            for (int i = 0; i < link.Length; i++)
            {
                XElement next = xml.Descendants(link[i]).FirstOrDefault();
                if (next == null) return null;
                xml = next;
            }
            return xml;
        }
    }
}

©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页