C#操作项目配置文件
前言
对于项目配置文件的读取和修改,.net 提供了ConfigurationManager(位于System.Configuration命名空间) 和WebConfigurationManager(位于System.Web.Configuration命名空间)两个类,能够很方便的操作项目的配置文件。另外,对于 Web 应用程序配置,建议使用 WebConfigurationManager 类,而不要使用 ConfigurationManager 类。
演示
下面是一个asp.net mvc5项目的项目配置文件
Web.config
<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=301880
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication1-20160513023153.mdf;Initial Catalog=aspnet-WebApplication1-20160513023153;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="MoocEntities" connectionString="metadata=res://*/Models.MOOC.csdl|res://*/Models.MOOC.ssdl|res://*/Models.MOOC.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=Mooc;user id=sa;password=aaaaaa;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
<modules>
<remove name="FormsAuthenticationModule" />
</modules>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
获取与修改appSettings配置节点下的属性值
WebConfigurationManager提供了AppSettings属性,便于访问配置文件中appSettings节点下的元素的value值
下图为Web.config中要获取值的元素

编写如下代码,并在第27行打上断点调试
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Configuration;
using System.Text;
using System.Collections.Specialized;
using System.Collections;
using System.Diagnostics;
namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{ string version = WebConfigurationManager.AppSettings["webpages:Version"];
string enabled = WebConfigurationManager.AppSettings["webpages:Enabled"];
string clientValidationEnabled = WebConfigurationManager.AppSettings["ClientValidationEnabled"];
string unobtrusiveJavaScriptEnabled = WebConfigurationManager.AppSettings["UnobtrusiveJavaScriptEnabled"];
Debug.WriteLine("配置信息如下");
Debug.WriteLine(version);
Debug.WriteLine(enabled);
Debug.WriteLine(clientValidationEnabled);
Debug.WriteLine(unobtrusiveJavaScriptEnabled);
return Content("");
} }
}
调试模式“输出”窗口

以WebConfigurationManager.AppSettings["ClientValidationEnabled"]="要修改的值" 可直接修改值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Configuration;
using System.Text;
using System.Collections.Specialized;
using System.Collections;
using System.Diagnostics;
namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{ string version = WebConfigurationManager.AppSettings["webpages:Version"];
string enabled = WebConfigurationManager.AppSettings["webpages:Enabled"];
WebConfigurationManager.AppSettings["ClientValidationEnabled"]="aaaaaaaaaaaaaaaaaaaaa";//动态修改value
string clientValidationEnabled = WebConfigurationManager.AppSettings["ClientValidationEnabled"];
string unobtrusiveJavaScriptEnabled = WebConfigurationManager.AppSettings["UnobtrusiveJavaScriptEnabled"];
Debug.WriteLine("配置信息如下");
Debug.WriteLine(version);
Debug.WriteLine(enabled);
Debug.WriteLine(clientValidationEnabled);
Debug.WriteLine(unobtrusiveJavaScriptEnabled);
return Content("");
} }
}
调试输出

连接字符串修改
/*获取数据库连接字符串*/
string moocConnection = WebConfigurationManager.ConnectionStrings["MoocEntities"].ConnectionString;
Debug.WriteLine("连接字符串如下:");//打印
Debug.WriteLine(moocConnection);//打印

未完待续-----
C#操作项目配置文件的更多相关文章
- springmvc 项目完整示例02 项目创建-eclipse创建动态web项目 配置文件 junit单元测试
包结构 所需要的jar包直接拷贝到lib目录下 然后选定 build path 之后开始写项目代码 配置文件 ApplicationContext.xml <?xml version=" ...
- Python中操作ini配置文件
这篇博客我主要想总结一下python中的ini文件的使用,最近在写python操作mysql数据库,那么作为测试人员测试的环境包括(测试环境,UAT环境,生产环境)每次需要连接数据库的ip,端口,都会 ...
- 微信小游戏 项目配置文件 project.config.json
一.项目配置文件project.config.json 小程序开发者工具在每个项目的根目录都会生成一个 project.config.json,在工具上做的任何配置都会写入到这个文件,当重新安装工具或 ...
- C#操作XML配置文件
代码为C#操作xml配置文件的范例类,函数SetValue用于向配置文件写入一个值,GetValue用于根据Key获取相应值。这种方法的配置文件不需要手动创建,程序在运行后会自动处理创建。 注意:1. ...
- spring-boot-plus项目配置文件(四)
spring-boot-plus项目配置文件 配置文件说明 配置说明 项目中配置文件主要使用yml格式 配置文件位置:spring-boot-plus\src\main\resources\confi ...
- springMVC项目配置文件
一.springMVC项目配置文件 1.web.xml文件全局配置 <servlet> <servlet-name> dispatcher </servlet-name& ...
- python pyyaml操作yaml配置文件
在测试工作中,可以使用yaml编写测试用例,执行测试用例时直接获取yaml中的用例数据进行测试(如:接口自动化测试) 1.什么是yaml 是一种可读的数据序列化语言,通常用于配置文件 非常简洁和强大, ...
- GIT·全局配置文件及项目配置文件
阅文时长 | 0.03分钟 字数统计 | 48.8字符 主要内容 | 1.引言&背景 2.声明与参考资料 『GIT·全局配置文件及项目配置文件』 编写人 | SCscHero 编写时间 | 2 ...
- VC++/MFC操作ini配置文件详解
在我们写的程序当中,总有一些配置信息需要保存下来,以便完成程序的功能,最简单的办法就是将这些信息写入INI文件中,程序初始化时再读入.具体应用如下: 一.将信息写入.INI文件中. 1.所用的WINA ...
随机推荐
- ViewPager的Adapter中视图重用
ViewPager的PagerAdapter不像ListView/GridView的BaseAdapter,它是没有内部视图重用机制的,也就是说我先inflate出来一个,然后调用destroyIte ...
- 技术之美[程序人生]我在IBM实习的日子
写这篇文章的时候,我已经在IBM正式工作了,看看上一篇博文的发布日期,才发现,我已经将近三个月没有更新博客了,多么惊人!为什么这么久?期间发生了很多事情.最重要的一件就是我大学毕业了!毕业的那么平淡, ...
- PropertyGrid—添加属性Tab
零.引言 PropertyGrid用来显示和编辑对象的属性,前面已经简单介绍了如何使用该控件和提供不同的属性编辑方法.前面主要讲如何使用该控件,但有时,该控件无法满足我们的需求,就需要对其进行扩展.本 ...
- JavaScript脚本语言的正则校验法
正则校验法有很多种类型,有些可能会比较复杂难记,我这里罗列了大家常用的几种方法,方便查询. //校验是否全由数字组成 function isShuZi(s) { var patrn=/^[0-9]{1 ...
- oc特有语法
分类 问题 1.什么是分类? 就是把一个类的功能,分出一部分来放在一个独立的文件中 2.分类的语法是什么样的? @interface Person(SuperMan) 3.分类与类是什么关系? 分类依 ...
- css3:border-radius圆角边框详解 (变圆 图片)
转:http://www.kuqin.com/shuoit/20141014/342620.html border-radius:50% 今天来聊聊这个border-radius属性,radius的英 ...
- HNOI2004 宠物收养所 解题报告
首先读完这题第一印象,是个裸题,很高兴.其次在打完代码之后,第二印象,很恶心,Treap的代码太长了,我今天下午敲了三遍,手都麻了. 废话不多说,正题.其实这个题不难,有几个点是很好的,首先,他的a值 ...
- function overloading/ declare function
Declare a function To declare a function without identifying the argument list, you can do it in thi ...
- spring的IOC,DI及案例详解
一:spring的基本特征 Spring是一个非常活跃的开源框架:它是一个基于Core来架构多层JavaEE系统的框架,它的主要目的是简化企业开发.Spring以一种非侵入式的方式来管理你的代码,Sp ...
- python核心编程-第三章-习题
1.这是python的语言特性,python先创建对象,在给变量赋值时,不需要定义变量的名称和类型,它实际是用变量引用对象.变量类型在给变量赋值时自动声明 2.原因类似变量无须声明类型 3.pytho ...