首页 > 代码库 > 修改和获取web.config或app.config文件appSettings配置节中的Add里的value属性 函数
修改和获取web.config或app.config文件appSettings配置节中的Add里的value属性 函数
1: /// <summary>
2: /// 修改web.config或app.config文件appSettings配置节中的Add里的value属性
3: /// </summary>
4: /// <remarks>
5: /// 注意,调用该函数后,会使整个Web Application重启,导致当前所有的会话丢失
6: /// </remarks>
7: /// <param name="key">要修改的键key</param>
8: /// <param name="strValue">修改后的value</param>
9: /// <exception cref="">找不到相关的键</exception>
10: /// <exception cref="">权限不够,无法保存到web.config文件中</exception>
11: public static void ModifyAppSettings(string key, string strValue)
12: {
13: string XPath = "/configuration/appSettings/add[@key=‘?‘]";
14: XmlDocument domConfig = new XmlDocument();
15:
16: domConfig.Load(Environment.CurrentDirectory + "/Application1.exe.config");
17: XmlNode addKey = domConfig.SelectSingleNode((XPath.Replace("?", key)));
18: if (addKey == null)
19: {
20: throw new ArgumentException("没有找到<add key=‘" + key + "‘ value=http://www.mamicode.com/.../>的配置节");
21: }
22: addKey.Attributes["value"].InnerText = strValue;
23: domConfig.Save(Environment.CurrentDirectory + "/Application1.exe.config");
24:
25: }
26:
27: /// <summary>
28: /// 获取web.config或app.config文件appSettings配置节中的Add里的value属性
29: /// </summary>
30: /// <param name="key">要修改的键key</param>
31: /// <param name="strValue">修改后的value</param>
32: /// <exception cref="">找不到相关的键</exception>
33: /// <exception cref="">权限不够,无法保存到web.config文件中</exception>
34: public static string GetAppSettings(string key)
35: {
36: string XPath = "/configuration/appSettings/add[@key=‘?‘]";
37: XmlDocument domConfig = new XmlDocument();
38:
39: domConfig.Load(Environment.CurrentDirectory + "/Application1.exe.config");
40: XmlNode addKey = domConfig.SelectSingleNode((XPath.Replace("?", key)));
41: if (addKey == null)
42: {
43: throw new ArgumentException("没有找到<add key=‘" + key + "‘ value=http://www.mamicode.com/.../>的配置节");
44: }
45: return addKey.Attributes["value"].InnerText;
46: }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。