是否有可能在運行時更改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. Ros動態配置文件
- 2. Docker動態配置文件
- 3. 動態配置文件URL
- 4. SSIS配置文件的動態位置
- 5. 動態設置錯誤操作的佈局文件
- 6. 配置文件路徑動態Grails中
- 7. Kohana動態編輯配置文件
- 8. tomcat-users.xml文件的動態配置
- 9. Struts.xml操作配置
- 10. 騾子動態設置肥皂操作
- 11. 配置logrotate狀態文件
- 12. Webpack配置(靜態文件)
- 13. 自動配置配置文件選擇
- 14. Spring.NET配置文件從一個文件夾動態加載
- 15. 配置文件的shell腳本做動態添加文件-ubuntu
- 16. 動態配置Spring郵件發件人
- 17. 從groovy配置文件動態分配值
- 18. 動態CRM插件 - 預操作更新
- 19. log4net動態配置
- 20. 動態配置mediaCodec
- 21. Log4J動態配置
- 22. 配置的靜態配置和動態配置
- 23. 帶有XML配置,動態文件位置的SSIS包源碼
- 24. Flume配置文件_滾動
- 25. Tinder配置文件動畫
- 26. 一個配置文件指定操作在Python
- 27. Jive 7:如何更改配置文件數據/操作?
- 28. 創建「編輯配置文件」操作CakePHP 2.0?
- 29. 從C#操作註冊表配置單元文件
- 30. 操作系統版本範圍的maven配置文件
可能重複[你如何在運行時修改web.config中的appSettings?](http://stackoverflow.com/questions/719928/how-do-you-modify-the-web-config-的AppSettings-在運行時) – M4N 2010-09-15 08:43:04