当前位置 : 主页 > 编程语言 > c语言 >

c# 更新web.config

来源:互联网 收集:自由互联 发布时间:2021-06-25
1 /// summary 2 /// 添加和更新配置文件web.config的appSettings,缺点是会删除注释 3 /// /summary 4 /// param name="key"/param 5 /// param name="value"/param 6 public static void AddUpdateAppSettings( string key, string valu
 1 /// <summary>
 2         /// 添加和更新配置文件web.config的appSettings,缺点是会删除注释
 3         /// </summary>
 4         /// <param name="key"></param>
 5         /// <param name="value"></param>
 6         public static void AddUpdateAppSettings(string key, string value)
 7         {
 8             try
 9             {
10                 var configFile = WebConfigurationManager.OpenWebConfiguration("~");
11                 var settings = configFile.AppSettings.Settings;
12                 try
13                 {
14                     if (settings[key] == null)
15                     {
16                         settings.Add(key, value);
17                     }
18                     else
19                     {
20                         settings[key].Value = value;
21                     }
22                     configFile.Save(ConfigurationSaveMode.Modified);
23                     ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
24                 }
25                 catch (Exception ex)
26                 {
27                     if (settings.Count != 0)
28                     {
29                         NLog.Error("配置文件写入失败,需要写入文件的配置信息:===============================");
30                         foreach (string tkey in settings.AllKeys)
31                         {
32                             string tvalue = settings[tkey].Value;
33                             NLog.Error(string.Format("Key: {0} Value: {1}", tkey, tvalue));
34                         }
35                     }
36                     throw;
37                 }
38             }
39             catch (Exception ex)
40             {
41                 NLog.Error("具体失败内容:" + ex);
42             }
43         }
44 
45         /// <summary>
46         /// 添加和更新web.config的appSettings,且不删除注释
47         /// </summary>
48         /// <param name="key"></param>
49         /// <param name="value"></param>
50         public static void UpdateAppSetting(string key, string value)
51         {
52             try
53             {
54                 Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
55                 SaveUsingXDocument(key, value, config.AppSettings.ElementInformation.Source);
56             }
57             catch (Exception ex)
58             {
59                 NLog.Error("更新配置失败" + ex);
60             }
61         }
62         private static void SaveUsingXDocument(string key, string value, string fileName)
63         {
64             XDocument document = XDocument.Load(fileName);
65             if (document.Root == null)
66             {
67                 return;
68             }
69             var appSettings = document.Root.Element("appSettings");
70             if (appSettings == null) return;
71             XElement appSetting = appSettings.Elements("add").FirstOrDefault(x => x.Attribute("key").Value == key);
72             if (appSetting != null)
73             {
74                 appSetting.Attribute("value").Value = value;
75             }
76             else
77             {
78                 XElement ele = new XElement("add", new XAttribute("key", key), new XAttribute("value", value));
79                 appSettings.Add(ele);
80             }
81             document.Save(fileName);
82         }
网友评论