DynamicConfig.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. namespace IndexFormula.Finance.Win
  2. {
  3. using System;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Windows.Forms;
  7. using System.Xml;
  8. public class DynamicConfig
  9. {
  10. private DynamicConfig()
  11. {
  12. }
  13. public static XmlDocument LoadFromFile()
  14. {
  15. return LoadFromFile(Application.ExecutablePath + ".config");
  16. }
  17. public static XmlDocument LoadFromFile(string Filename)
  18. {
  19. XmlDocument document = new XmlDocument();
  20. if (File.Exists(Filename))
  21. {
  22. document.Load(Filename);
  23. return document;
  24. }
  25. return null;
  26. }
  27. public static void Save(Control C)
  28. {
  29. XmlDocument xd = LoadFromFile();
  30. try
  31. {
  32. if (C is ChartWinControl)
  33. {
  34. (C as ChartWinControl).SaveChartProperties();
  35. }
  36. Save(xd, C);
  37. }
  38. finally
  39. {
  40. SaveToFile(xd);
  41. }
  42. }
  43. public static void Save(XmlDocument xd, Control C)
  44. {
  45. XmlNode node = xd.SelectSingleNode("/configuration/appSettings");
  46. for (int i = 0; i < node.ChildNodes.Count; i++)
  47. {
  48. if (node.ChildNodes[i] is XmlElement)
  49. {
  50. XmlElement element = node.ChildNodes[i] as XmlElement;
  51. string name = element.GetAttribute("key").ToString();
  52. string str2 = "";
  53. int index = name.IndexOf('.');
  54. if (index > 0)
  55. {
  56. str2 = name.Substring(0, index);
  57. name = name.Substring(index + 1);
  58. }
  59. if (C.Name == str2)
  60. {
  61. try
  62. {
  63. object obj2 = C.GetType().InvokeMember(name, BindingFlags.GetProperty, null, C, new object[0]);
  64. if (obj2 != null)
  65. {
  66. element.SetAttribute("value", obj2.ToString());
  67. }
  68. }
  69. catch
  70. {
  71. }
  72. }
  73. }
  74. }
  75. }
  76. public static void Save(XmlDocument xd, string Key, string Value)
  77. {
  78. Save(xd, "appSettings", Key, Value);
  79. }
  80. public static void Save(XmlDocument xd, string ConfigTag, string Key, string Value)
  81. {
  82. XmlNode node = xd.SelectSingleNode("/configuration/" + ConfigTag);
  83. if (node == null)
  84. {
  85. node = xd.SelectSingleNode("/configuration");
  86. XmlNode node2 = xd.CreateNode(XmlNodeType.Element, ConfigTag, "");
  87. node.AppendChild(node2);
  88. node = node2;
  89. }
  90. for (int i = 0; i < node.ChildNodes.Count; i++)
  91. {
  92. if (node.ChildNodes[i] is XmlElement)
  93. {
  94. XmlElement element = node.ChildNodes[i] as XmlElement;
  95. if ((element.GetAttribute("key").ToString() == Key) && (Value != null))
  96. {
  97. element.SetAttribute("value", Value.ToString());
  98. return;
  99. }
  100. }
  101. }
  102. XmlElement newChild = (XmlElement) xd.CreateNode(XmlNodeType.Element, "add", "");
  103. newChild.SetAttribute("key", Key);
  104. newChild.SetAttribute("value", Value);
  105. node.AppendChild(newChild);
  106. }
  107. public static void SaveToFile(XmlDocument xd)
  108. {
  109. string filename = Application.ExecutablePath + ".config";
  110. xd.Save(filename);
  111. }
  112. }
  113. }