Asp.net使用代码修改配置文件的节点值
生活随笔
收集整理的這篇文章主要介紹了
Asp.net使用代码修改配置文件的节点值
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用代碼修改配置文件的方法:
1、打開配置文件寫入的權限
2、先按節點名稱長到要修改的節點,然后刪除,緊接著將有新值的節點添加回去
3、關閉配置文件寫入的權限
修改Appsetting節點的值,修改其它節點的方法也差不多,也是找到要修改的節點刪除掉然后新新值的節點加上
public bool UpdateAppSettings(string key, string value){bool reuslt = false;try{Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);SetFileAccess(config.FilePath + "", false);ConfigurationSection sections = config.GetSection("appSettings");bool isSet = false;for (int i = 0; i < ((System.Configuration.AppSettingsSection)(sections)).Settings.Count; i++){string itemkey = ((System.Configuration.AppSettingsSection)(sections)).Settings.AllKeys[i];if (itemkey == key){((System.Configuration.AppSettingsSection)(sections)).Settings.Remove(key);((System.Configuration.AppSettingsSection)(sections)).Settings.Add(key, value);isSet = true;break;}}if (!isSet){((System.Configuration.AppSettingsSection)(sections)).Settings.Add(key, value);}config.Save();ConfigurationManager.RefreshSection("appSettings");SetFileAccess(config.FilePath + "", true);reuslt = true;}catch (Exception ex){LogNet.Log.WriteLog("UpdateAppSettings", ex);}return reuslt;} View Code修改配置文件的讀寫權限
protected void SetFileAccess(string path, bool isReadOnly){FileInfo fi = new FileInfo(path);if (fi.IsReadOnly != isReadOnly)fi.IsReadOnly = isReadOnly;} View Code?
轉載于:https://www.cnblogs.com/yonsy/p/5606920.html
總結
以上是生活随笔為你收集整理的Asp.net使用代码修改配置文件的节点值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RedHat 7配置FTP服务
- 下一篇: ubuntu服务器mysql管理 第一弹