AppConfigureHelper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. using System.IO;
  7. using System.Xml;
  8. using System.Reflection;
  9. using System.Diagnostics;
  10. namespace Uestc.Auto6.Dso.ComModel
  11. {
  12. /// <summary>
  13. /// 支持AppSettings和ConnectionStrings节点,如需其他节点请自行扩展
  14. /// </summary>
  15. public class AppConfigureHelper
  16. {
  17. //AppSettings
  18. public static Hashtable AppSettings => GetKeyValuePair("appSettings", "key", "value");
  19. //ConnectionStrings
  20. public static Hashtable ConnectionStrings => GetKeyValuePair("connectionStrings", "name", "connectionString");
  21. /// <summary>
  22. /// 根据节点名,子节点名,获取指定值
  23. /// </summary>
  24. /// <param name="section">对应节点</param>
  25. /// <param name="key">key或者name</param>
  26. /// <param name="value">key或者name的值</param>
  27. /// <returns>key或者name对应value值</returns>
  28. private static Hashtable GetKeyValuePair(String section, String key, String value)
  29. {
  30. Hashtable settings = new(5);
  31. var asm = typeof(AppConfigureHelper).Assembly;
  32. //获取当前DLL的路径,添加 .config 后缀,得到配置文件路径
  33. String cfgpath = asm.Location + ".config";
  34. if (!File.Exists(cfgpath))
  35. {
  36. //获取进程可执行文件的路径,添加 .config 后缀,得到配置文件路径
  37. cfgpath = Assembly.GetEntryAssembly()?.Location + ".config";
  38. if (!File.Exists(cfgpath))
  39. return settings;
  40. }
  41. var cfg = new XmlDocument();
  42. FileStream? fs = null;
  43. try
  44. {
  45. fs = new FileStream(cfgpath, FileMode.Open, FileAccess.Read);
  46. cfg.Load(new XmlTextReader(fs));
  47. XmlNodeList nodes = cfg.GetElementsByTagName(section);
  48. foreach (XmlNode node in nodes)
  49. {
  50. foreach (XmlNode subnode in node.ChildNodes)
  51. {
  52. XmlAttributeCollection? attributes = subnode.Attributes;
  53. if (attributes?[key] is not null)
  54. {
  55. settings.Add(attributes[key]!.Value, attributes[value]?.Value);
  56. }
  57. }
  58. }
  59. }
  60. //catch (FileNotFoundException es)
  61. //{
  62. // throw es;
  63. //}
  64. finally
  65. {
  66. fs?.Close();
  67. }
  68. return settings;
  69. }
  70. //public static void SetAppSettings(String key, String value)
  71. //{
  72. // SetNameAndValue("appSettings", "key", key, value);
  73. //}
  74. //public static void SetConnectionStrings(String key, String value)
  75. //{
  76. // SetNameAndValue("connectionStrings", "name", key, value);
  77. //}
  78. //private static void SetNameAndValue(String section, String key, String keyname, String value)
  79. //{
  80. // String cfgpath = typeof(AppConfigureHelper).Assembly.Location + ".config";
  81. // try
  82. // {
  83. // XmlDocument cfgfile = new();
  84. // cfgfile.Load(cfgpath);
  85. // XmlNodeList nodes = cfgfile.GetElementsByTagName(section);
  86. // foreach (XmlNode node in nodes)
  87. // {
  88. // foreach (XmlNode subnode in node.ChildNodes)
  89. // {
  90. // XmlAttributeCollection? attributes = subnode.Attributes;
  91. // if (attributes != null)
  92. // {
  93. // if (attributes.GetNamedItem(key)?.InnerText == keyname)
  94. // {
  95. // var v = attributes.GetNamedItem("value");
  96. // if (v is not null)
  97. // v.InnerText = value;
  98. // }
  99. // }
  100. // }
  101. // }
  102. // cfgfile.Save(cfgpath);
  103. // }
  104. // catch (FileNotFoundException)
  105. // {
  106. // throw;
  107. // }
  108. //}
  109. }
  110. }