| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- namespace IndexFormula.Finance.Win
- {
- using System;
- using System.IO;
- using System.Reflection;
- using System.Windows.Forms;
- using System.Xml;
- public class DynamicConfig
- {
- private DynamicConfig()
- {
- }
- public static XmlDocument LoadFromFile()
- {
- return LoadFromFile(Application.ExecutablePath + ".config");
- }
- public static XmlDocument LoadFromFile(string Filename)
- {
- XmlDocument document = new XmlDocument();
- if (File.Exists(Filename))
- {
- document.Load(Filename);
- return document;
- }
- return null;
- }
- public static void Save(Control C)
- {
- XmlDocument xd = LoadFromFile();
- try
- {
- if (C is ChartWinControl)
- {
- (C as ChartWinControl).SaveChartProperties();
- }
- Save(xd, C);
- }
- finally
- {
- SaveToFile(xd);
- }
- }
- public static void Save(XmlDocument xd, Control C)
- {
- XmlNode node = xd.SelectSingleNode("/configuration/appSettings");
- for (int i = 0; i < node.ChildNodes.Count; i++)
- {
- if (node.ChildNodes[i] is XmlElement)
- {
- XmlElement element = node.ChildNodes[i] as XmlElement;
- string name = element.GetAttribute("key").ToString();
- string str2 = "";
- int index = name.IndexOf('.');
- if (index > 0)
- {
- str2 = name.Substring(0, index);
- name = name.Substring(index + 1);
- }
- if (C.Name == str2)
- {
- try
- {
- object obj2 = C.GetType().InvokeMember(name, BindingFlags.GetProperty, null, C, new object[0]);
- if (obj2 != null)
- {
- element.SetAttribute("value", obj2.ToString());
- }
- }
- catch
- {
- }
- }
- }
- }
- }
- public static void Save(XmlDocument xd, string Key, string Value)
- {
- Save(xd, "appSettings", Key, Value);
- }
- public static void Save(XmlDocument xd, string ConfigTag, string Key, string Value)
- {
- XmlNode node = xd.SelectSingleNode("/configuration/" + ConfigTag);
- if (node == null)
- {
- node = xd.SelectSingleNode("/configuration");
- XmlNode node2 = xd.CreateNode(XmlNodeType.Element, ConfigTag, "");
- node.AppendChild(node2);
- node = node2;
- }
- for (int i = 0; i < node.ChildNodes.Count; i++)
- {
- if (node.ChildNodes[i] is XmlElement)
- {
- XmlElement element = node.ChildNodes[i] as XmlElement;
- if ((element.GetAttribute("key").ToString() == Key) && (Value != null))
- {
- element.SetAttribute("value", Value.ToString());
- return;
- }
- }
- }
- XmlElement newChild = (XmlElement) xd.CreateNode(XmlNodeType.Element, "add", "");
- newChild.SetAttribute("key", Key);
- newChild.SetAttribute("value", Value);
- node.AppendChild(newChild);
- }
- public static void SaveToFile(XmlDocument xd)
- {
- string filename = Application.ExecutablePath + ".config";
- xd.Save(filename);
- }
- }
- }
|