修改和擷取web.config或app.config檔案appSettings配置節中的Add裡的value屬性 函數

來源:互聯網
上載者:User

標籤:style   class   code   ext   color   width   

   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=.../>的配置節");
  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=.../>的配置節");
  44:      }
  45:      return addKey.Attributes["value"].InnerText;
  46:  }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.