1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Xml;
5
6 namespace Higo.Controls.Config
7 {
8 /// <summary>
9 /// 配置文件辅助类
10 /// </summary>
11 public class ConfigHelper
12 {
13 #region 私有域
14
15 private readonly string _filePath = null;
16 private Dictionary<string, FormConfig> _configData = new Dictionary<string, FormConfig>();
17
18 #endregion
19
20 #region 构造函数
21
22 ConfigHelper()
23 {
24 _filePath = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "control.config");
25 InitConfig();
26 }
27
28 #endregion
29
30 #region 自定义方法
31
32 private void InitConfig()
33 {
34 try
35 {
36 // 读取系统配置文件,获取系统配置信息
37 XmlDocument doc = new XmlDocument();
38 doc.Load(_filePath);
39 if (doc != null)
40 {
41 XmlNodeList nodes = doc.SelectNodes("//forms//form");
42 foreach (XmlNode formNode in nodes)
43 {
44 FormConfig form = new FormConfig();
45 foreach (XmlAttribute attr in formNode.Attributes)
46 {
47 form.Add(attr.Name, attr.Value);
48 }
49
50 XmlNodeList controlsNodes = formNode.SelectNodes("//control");
51 foreach (XmlNode detailNode in controlsNodes)
52 {
53 ControlConfig control = new ControlConfig(form);
54 foreach (XmlAttribute attr in detailNode.Attributes)
55 {
56 control.Add(attr.Name, attr.Value);
57 }
58
59 form.Controls.Add(control);
60 }
61
62 _configData.Add(form.GetValue("Name"), form);
63 }
64 }
65 }
66 catch (Exception ex)
67 {
68 // TODO:记录日志
69 }
70 }
71
72 #endregion
73
74 #region 单例模式辅助类
75
76 class ConfigHelperer
77 {
78 static ConfigHelperer()
79 {
80 }
81
82 internal static readonly ConfigHelper instance = new ConfigHelper();
83 }
84
85 /// <summary>
86 /// 通过单例调用配置访问接口
87 /// </summary>
88 public static ConfigHelper Instance
89 {
90 get
91 {
92 return ConfigHelperer.instance;
93 }
94 }
95
96 #endregion
97
98 #region 索引器
99
100 /// <summary>
101 /// 根据窗体名称,获取窗体配置
102 /// </summary>
103 /// <param name="Name">窗体名称</param>
104 /// <returns>窗体配置对象</returns>
105 public FormConfig this[string name]
106 {
107 get
108 {
109 if (_configData.ContainsKey(name))
110 {
111 return _configData[name];
112 }
113
114 return null;
115 }
116 }
117
118 #endregion
119
120 #region 公有方法
121
122 public void SaveConfig()
123 {
124 XmlDocument xDoc = new XmlDocument();
125 xDoc.Load(_filePath);
126
127 XmlNode xNode;
128 XmlElement xElem;
129 XmlNodeList xNodeList;
130
131 foreach (KeyValuePair<string, FormConfig> kp in _configData)
132 {
133 string name = kp.Key;
134 FormConfig form = kp.Value;
135
136 xElem = (XmlElement)xDoc.SelectSingleNode("//forms//form[@Name='" + name + "']");
137 if (xElem != null)
138 {
139 foreach (KeyValuePair<string, string> formKP in form.Config)
140 {
141 xElem.SetAttribute(formKP.Key, formKP.Value);
142 }
143
144 foreach (ControlConfig control in form.Controls)
145 {
146 xElem = (XmlElement)xDoc.SelectSingleNode("//forms//form//control[@Name='" + control["Name"] + "']");
147 if (xElem != null)
148 {
149 foreach (KeyValuePair<string, string> controlKP in control.Config)
150 {
151 xElem.SetAttribute(controlKP.Key, controlKP.Value);
152 }
153 }
154 }
155 }
156 }
157
158 xDoc.Save(_filePath);
159 }
160
161 #endregion
162 }
163 }