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
;
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
);
}
}
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
);
}
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
;
}
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;
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
;
}
}
}
转载请注明原文地址: https://lol.8miu.com/read-9794.html