asp.net代码中修改web.config节点
生活随笔
收集整理的這篇文章主要介紹了
asp.net代码中修改web.config节点
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在有些情況下,要在代碼中讀取一種全局變量,把這種全局變量放在web.config是一種常見的手段。但是這個變量不會一個固定的值,會根據實際情況而發生變化,比如在需要讀取一個配置文件的路徑,而這個路徑是站點發布的實際硬盤路徑,如果直接是編譯時狀態,沒有問題。但是如果站點iis更換路徑,就需要修改這個web.config中的參數。如果能將這個編譯時狀態修改為運行時狀態,那將更為合理和方便。這就需要存在一種在代碼中能夠動態修改web.config的方案。
查看代碼 1 /// <summary> 2 /// 寫入web.config 3 /// </summary> 4 /// <param name="item">appSettings等</param> 5 /// <param name="key">鍵</param> 6 /// <param name="value">值</param> 7 public void WriteConfig(string item, string key, string value) 8 { 9 if (item == "") 10 { 11 item = "appSettings"; 12 } 13 Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath); 14 AppSettingsSection appSection = (AppSettingsSection)config.GetSection(item); 15 if (appSection.Settings[key] == null) 16 { 17 appSection.Settings.Add(key, value); 18 config.Save(); 19 } 20 else 21 { 22 appSection.Settings.Remove(key); 23 appSection.Settings.Add(key, value); 24 config.Save(); 25 } 26 }?
?
轉載于:https://www.cnblogs.com/xtechnet/archive/2012/06/04/2535080.html
總結
以上是生活随笔為你收集整理的asp.net代码中修改web.config节点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Web架构师成长之路
- 下一篇: CMMI for Development