是否有可能在運行時更改web.config的內容?動態操作配置文件
Q
動態操作配置文件
0
A
回答
0
使用WebConfigurationManager類的另一種方法。
Configuration cfg = WebConfigurationManager.OpenWebConfiguration("~");
ConnectionStringSettings consettings = cfg.ConnectionStrings.ConnectionStrings["conkey"];
consettings.ConnectionString = "updated value";
cfg.Save();
的
1
是的。
的安全的方法是寫appSettings
:Writing to Your .NET Application's Config File
但是你可以also hack it(不這樣做)。
0
我試過以下代碼來在運行時更新web.config文件。
比方說web.config中有這樣
<connectionStrings>
<add name="conkey" connectionString="old value" />
</connectionStrings>
的關鍵,這裏是C#代碼更新web.config文件。
string path = Server.MapPath("Web.config");
string newConnectionString = "updated value"; // Updated Value
XmlDocument xDoc = new XmlDocument();
xDoc.Load(path);
XmlNodeList nodeList = xDoc.GetElementsByTagName("connectionStrings");
XmlNodeList nodeconnectionStrings = nodeList[0].ChildNodes;
XmlAttributeCollection xmlAttCollection = nodeconnectionStrings[0].Attributes;
xmlAttCollection[1].InnerXml = newConnectionString; // for value attribute
xDoc.Save(path); // saves the web.config file
此代碼適用於我。不過建議不要這樣做。
相關問題
- 1. Docker動態配置文件
- 2. 配置文件路徑動態Grails中
- 3. tomcat-users.xml文件的動態配置
- 4. Struts.xml操作配置
- 5. 騾子動態設置肥皂操作
- 6. 配置logrotate狀態文件
- 7. 自動配置配置文件選擇
- 8. 動態配置mediaCodec
- 9. 配置的靜態配置和動態配置
- 10. 帶有XML配置,動態文件位置的SSIS包源碼
- 11. Tinder配置文件動畫
- 12. Flume配置文件_滾動
- 13. 操作Apache2配置文件的好的庫是什麼?
- 14. 創建「編輯配置文件」操作CakePHP 2.0?
- 15. 一個配置文件指定操作在Python
- 16. 使用Java操作BIND 9.x配置文件
- 17. 從C#操作註冊表配置單元文件
- 18. 操作系統版本範圍的maven配置文件
- 19. 更改配置文件和複雜註釋操作
- 20. 如何在文件系統任務中配置「複製文件」和「移動文件」操作?
- 21. 從靜態配置移動到動態配置
- 22. 動態隊列操作
- 23. WebGL動態模型操作
- 24. 動態操作的JavaScript
- 25. 動態創建操作符
- 26. Mathematica動態列表操作
- 27. 在CakePHP中動態加載插件配置文件3
- 28. 動態生成咕嚕插件配置
- 29. Java Spring自動裝配配置文件
- 30. 操作文件
可能重複[你如何在運行時修改web.config中的appSettings?](http://stackoverflow.com/questions/719928/how-do-you-modify-the-web-config-的AppSettings-在運行時) – M4N 2010-09-15 08:43:04