首頁  >  文章  >  後端開發  >  asp.net程式碼中修改web.config節點的具體方法

asp.net程式碼中修改web.config節點的具體方法

高洛峰
高洛峰原創
2017-01-13 13:35:551342瀏覽

但是這個變數不會一個固定的值,會根據實際情況而發生變化,例如在需要讀取一個設定檔的路徑,而這個路徑是網站發佈的實際硬碟路徑,如果直接是編譯時狀態,沒有問題。但是如果網站iis更換路徑,就需要修改這個web.config中的參數。如果能將這個編譯時狀態修改為執行時間狀態,將會更為合理和方便。這就需要存在一個在程式碼中能夠動態修改web.config的方案。
  程式碼

 /// <summary>
          /// 写入web.config
          /// </summary>
          /// <param name="item">appSettings等</param>
          /// <param name="key">键</param>
          /// <param name="value">值</param>
          public void WriteConfig(string item, string key, string value)
          {
              if (item == "")
             {
                 item = "appSettings";
             }
             Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath);
             AppSettingsSection appSection = (AppSettingsSection)config.GetSection(item);
             if (appSection.Settings[key] == null)
             {
                 appSection.Settings.Add(key, value);
                 config.Save();
             }
             else
             {
                 appSection.Settings.Remove(key);
                 appSection.Settings.Add(key, value);
                 config.Save();
            }
         }

更多asp.net程式碼中修改web.config節點的具體方法相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn