当前位置:   article > 正文

读取AppConfig_读取app.config

读取app.config
  1. using System.Xml;
  2. using System.IO;
  3. using System;
  4. namespace Framework.Common
  5. {
  6. /// <summary>
  7. /// 用于获取或设置Web.config/*.exe.config中节点数据的辅助类
  8. /// </summary>
  9. public sealed class AppConfig
  10. {
  11. private string filePath;
  12. /// <summary>
  13. /// 从当前目录中按顺序检索Web.Config和*.App.Config文件。
  14. /// 如果找到一个,则使用它作为配置文件;否则会抛出一个ArgumentNullException异常。
  15. /// </summary>
  16. public AppConfig()
  17. {
  18. string webconfig = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Web.Config");
  19. string appConfig = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile.Replace(".vshost", "");
  20. if (File.Exists(webconfig))
  21. {
  22. filePath = webconfig;
  23. }
  24. else if (File.Exists(appConfig))
  25. {
  26. filePath = appConfig;
  27. }
  28. else
  29. {
  30. throw new ArgumentNullException("没有找到Web.Config文件或者应用程序配置文件, 请指定配置文件");
  31. }
  32. }
  33. /// <summary>
  34. /// 用户指定具体的配置文件路径
  35. /// </summary>
  36. /// <param name="configFilePath">配置文件路径(绝对路径)
  37. public AppConfig(string configFilePath)
  38. {
  39. filePath = configFilePath;
  40. }
  41. /// <summary>
  42. /// 设置程序的config文件
  43. /// </summary>
  44. /// <param name="keyName">键名
  45. /// <param name="keyValue">键值
  46. public void AppConfigSet(string keyName, string keyValue)
  47. {
  48. //由于存在多个Add键值,使得访问appSetting的操作不成功,故注释下面语句,改用新的方式
  49. /*
  50. string xpath = "//add[@key='" + keyName + "']";
  51. XmlDocument document = new XmlDocument();
  52. document.Load(filePath);
  53. XmlNode node = document.SelectSingleNode(xpath);
  54. node.Attributes["value"].Value = keyValue;
  55. document.Save(filePath);
  56. */
  57. XmlDocument document = new XmlDocument();
  58. document.Load(filePath);
  59. XmlNodeList nodes = document.GetElementsByTagName("add");
  60. for (int i = 0; i < nodes.Count; i++)
  61. {
  62. //获得将当前元素的key属性
  63. XmlAttribute attribute = nodes[i].Attributes["key"];
  64. //根据元素的第一个属性来判断当前的元素是不是目标元素
  65. if (attribute != null && (attribute.Value == keyName))
  66. {
  67. attribute = nodes[i].Attributes["value"];
  68. //对目标元素中的第二个属性赋值
  69. if (attribute != null)
  70. {
  71. attribute.Value = keyValue;
  72. break;
  73. }
  74. }
  75. }
  76. document.Save(filePath);
  77. }
  78. /// <summary>
  79. /// 读取程序的config文件的键值。
  80. /// 如果键名不存在,返回空
  81. /// </summary>
  82. /// <param name="keyName">键名
  83. /// <returns></returns>
  84. public string AppConfigGet(string keyName)
  85. {
  86. string strReturn = string.Empty;
  87. try
  88. {
  89. XmlDocument document = new XmlDocument();
  90. document.Load(filePath);
  91. XmlNodeList nodes = document.GetElementsByTagName("add");
  92. for (int i = 0; i < nodes.Count; i++)
  93. {
  94. //获得将当前元素的key属性
  95. XmlAttribute attribute = nodes[i].Attributes["key"];
  96. //根据元素的第一个属性来判断当前的元素是不是目标元素
  97. if (attribute != null && (attribute.Value == keyName))
  98. {
  99. attribute = nodes[i].Attributes["value"];
  100. if (attribute != null)
  101. {
  102. strReturn = attribute.Value;
  103. break;
  104. }
  105. }
  106. }
  107. }
  108. catch
  109. {
  110. ;
  111. }
  112. return strReturn;
  113. }
  114. /// <summary>
  115. /// 获取指定键名中的子项的值
  116. /// </summary>
  117. /// <param name="keyName">键名
  118. /// <param name="subKeyName">以分号(;)为分隔符的子项名称
  119. /// <returns>对应子项名称的值(即是=号后面的值)</returns>
  120. public string GetSubValue(string keyName, string subKeyName)
  121. {
  122. string connectionString = AppConfigGet(keyName).ToLower();
  123. string[] item = connectionString.Split(new char[] { ';' });
  124. for (int i = 0; i < item.Length; i++)
  125. {
  126. string itemValue = item[i].ToLower();
  127. if (itemValue.IndexOf(subKeyName.ToLower()) >= 0) //如果含有指定的关键字
  128. {
  129. int startIndex = item[i].IndexOf("="); //等号开始的位置
  130. return item[i].Substring(startIndex + 1); //获取等号后面的值即为Value
  131. }
  132. }
  133. return string.Empty;
  134. }
  135. #region 一些常用的配置项属性
  136. /// <summary>
  137. /// 从配置文件获取权限系统链接(配置项HWSecurity的值)
  138. /// </summary>
  139. public string HWSecurity
  140. {
  141. get
  142. {
  143. return AppConfigGet("HWSecurity");
  144. }
  145. }
  146. /// <summary>
  147. /// 系统的标识ID(配置项System_ID的值)
  148. /// </summary>
  149. public string System_ID
  150. {
  151. get
  152. {
  153. return AppConfigGet("System_ID");
  154. }
  155. }
  156. /// <summary>
  157. /// 应用程序名称(配置项ApplicationName的值)
  158. /// </summary>
  159. public string AppName
  160. {
  161. get
  162. {
  163. return AppConfigGet("ApplicationName");
  164. }
  165. }
  166. /// <summary>
  167. /// 软件厂商名称(配置项Manufacturer的值)
  168. /// </summary>
  169. public string Manufacturer
  170. {
  171. get
  172. {
  173. return AppConfigGet("Manufacturer");
  174. }
  175. }
  176. /// <summary>
  177. /// 设置程序的config文件的Enterprise Library的数据库链接地址
  178. /// </summary>
  179. /// <param name="keyName">键名
  180. /// <param name="keyValue">键值
  181. public void SetConnectionString(string keyName, string keyValue)
  182. {
  183. XmlDocument document = new XmlDocument();
  184. document.Load(filePath);
  185. XmlNodeList nodes = document.GetElementsByTagName("add");
  186. for (int i = 0; i < nodes.Count; i++)
  187. {
  188. //获得将当前元素的name属性
  189. XmlAttribute att = nodes[i].Attributes["name"];
  190. //根据元素的第一个属性来判断当前的元素是不是目标元素
  191. if (att != null && (att.Value == keyName))
  192. {
  193. att = nodes[i].Attributes["connectionString"];
  194. if (att != null)
  195. {
  196. att.Value = keyValue;
  197. break;
  198. }
  199. }
  200. }
  201. document.Save(filePath);
  202. }
  203. /// <summary>
  204. /// 读取程序的config文件Enterprise Library的数据库链接地址
  205. /// </summary>
  206. /// <param name="keyName">键名
  207. /// <returns></returns>
  208. public string GetConnectionString(string keyName)
  209. {
  210. string strReturn = string.Empty;
  211. try
  212. {
  213. XmlDocument document = new XmlDocument();
  214. document.Load(filePath);
  215. XmlNodeList nodes = document.GetElementsByTagName("add");
  216. for (int i = 0; i < nodes.Count; i++)
  217. {
  218. //获得将当前元素的key属性
  219. XmlAttribute att = nodes[i].Attributes["name"];
  220. //根据元素的第一个属性来判断当前的元素是不是目标元素
  221. if (att != null && (att.Value == keyName))
  222. {
  223. att = nodes[i].Attributes["connectionString"];
  224. if (att != null)
  225. {
  226. strReturn = att.Value;
  227. break;
  228. }
  229. }
  230. }
  231. }
  232. catch
  233. { ; }
  234. return strReturn;
  235. }
  236. /// <summary>
  237. /// 获取数据库配置信息
  238. /// </summary>
  239. /// <param name="keyName">节点名称
  240. /// <returns></returns>
  241. public DatabaseInfo GetDatabaseInfo(string keyName)
  242. {
  243. string connectionString = GetConnectionString(keyName);
  244. return new DatabaseInfo(connectionString);
  245. }
  246. /// <summary>
  247. /// 设置数据库配置信息
  248. /// </summary>
  249. /// <param name="keyName">
  250. /// <param name="databaseInfo">
  251. public void SetDatabaseInfo(string keyName, DatabaseInfo databaseInfo)
  252. {
  253. SetConnectionString(keyName, databaseInfo.ConnectionString);
  254. }
  255. #endregion
  256. }
  257. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/239402
推荐阅读
相关标签
  

闽ICP备14008679号