.Net 对App.config和Web.config的访问操作(增、删、读、改)
2024-09-09 13:48:28
一。首先引用Configuration
1)App.config如下:
using System.Configuration;//若果还没有Configuration,右键引用文件夹添加引用,在.NET中找到System.configuration添加
2)Web.config如下:
using System.Configuration;
using System.Web.Configuration;
1)App.config页面代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="name" value="张三"/>
</appSettings>
</configuration>
2)Web.config页面代码如下:
<?xml version="1.0"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<appSettings>
<add key="name" value="张三"/>
</appSettings>
</configuration>
二。增加
1)App.config示例:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection appsettings = (AppSettingsSection)config.GetSection("appSettings");
appsettings.Settings.Add("color", "blue");
config.Save();
string str = ConfigurationManager.AppSettings["color"];
2)Web.config示例:
Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
AppSettingsSection app = config.AppSettings;
app.Settings.Add("color", "blue");
config.Save(ConfigurationSaveMode.Modified);
string str=WebConfigurationManager.AppSettings["color"];
三。删除
App.config示例:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection appsettings = (AppSettingsSection)config.GetSection("appSettings");
appsettings.Settings.Remove("name");
config.Save();
ConfigurationManager.RefreshSection("name");
string str = ConfigurationManager.AppSettings["name"];
Web.config示例:
Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
AppSettingsSection app = config.AppSettings;
app.Settings.Remove("name");
config.Save();
string str1=WebConfigurationManager.AppSettings["name"];
四。读取
1)App.config示例:
string str=ConfigurationManager.AppSettings["name"];
2)Web.config示例:
string str=WebConfigurationManager.AppSettings["color"];
五。修改
App.config示例:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["name"].Value = "李四";
config.Save();
string str = ConfigurationManager.AppSettings["name"];
Web.config示例:
Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
config.AppSettings.Settings["name"].Value = "李四";
config.Save();
string str = ConfigurationManager.AppSettings["name"];
最新文章
- java function retry wrapper
- 使用HTML5新支持的搭建WebRtc环境来作为视频通讯
- CentOS防火墙问题
- javascript设计模式实践之代理模式--图片预加载
- js快速排序方法
- Web Api其中的PUT功能演示
- Java完成最简单的WebService创建及使用(REST方式,Jersey框架)
- hdu 4163 Stock Prices 水
- Aggressive cows 二分不仅仅是查找
- 一次完整的http请求所需要完成的步骤
- div footer标签css实现位于页面底部固定
- beanutils中Lazy
- RoundedImageView使用吐槽心得(RoundedImageView与Glide加载图片,第一次加载无法圆角问题)
- 201521123040《Java程序设计》第1周学习总结
- angularjs bind与model配合双向绑定 表达式方法输出
- 实现string到double的转换
- Java实现附近地点搜索
- java 新手指南
- LaTeX入门
- VsCode云端版本
热门文章
- 编程模式之观察者模式(Observer)
- css 去除点击之后的虚线
- ArcGIS Engine 刷新问题
- java中 DigestUtils.md5Hex 的c#实现
- 介绍一款非常适合做微网站并且免费的CMS系统
- 循序渐进Python3(十)-- 0 -- RabbitMQ
- $.grep(array, callback, [invert])过滤,常用
- 【项目】搜索广告CTR预估(二)
- myeclipse 手动安装 lombok
- TCP数据包的封包和拆包