zoukankan      html  css  js  c++  java
  • c# 更新web.config

     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         }
  • 相关阅读:
    面试问题 集锦
    减少 lwip 消耗 的 RAM
    Blocking Master Example QT 自带 的 serial 即 串口 例子
    32位 的变量 用于表示 ms ,可以表示多少天那?
    centos 腾讯云 今天买了 18个月
    Linux BLE 基于 树莓派
    树莓派 4G模块 PPP 拨号 NDIS 拨号
    linux备份还原命令
    centos7中/tmp文件保存天数
    centos7查看可登陆用户
  • 原文地址:https://www.cnblogs.com/mantishell/p/11663504.html
Copyright © 2011-2022 走看看