前言

对于项目配置文件的读取和修改,.net 提供了ConfigurationManager(位于System.Configuration命名空间) 和WebConfigurationManager(位于System.Web.Configuration命名空间)两个类,能够很方便的操作项目的配置文件。另外,对于 Web 应用程序配置,建议使用 WebConfigurationManager 类,而不要使用 ConfigurationManager 类。

演示

下面是一个asp.net mvc5项目的项目配置文件

Web.config

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!--
 3   有关如何配置 ASP.NET 应用程序的详细信息,请访问
 4   http://go.microsoft.com/fwlink/?LinkId=301880
 5   -->
 6 <configuration>
 7   <configSections>
 8     <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
 9     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
10   </configSections>
11   <connectionStrings>
12     <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" />
13     <add name="MoocEntities" connectionString="metadata=res://*/Models.MOOC.csdl|res://*/Models.MOOC.ssdl|res://*/Models.MOOC.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=Mooc;user id=sa;password=aaaaaa;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
14   </connectionStrings>
15   <appSettings>
16     <add key="webpages:Version" value="3.0.0.0" />
17     <add key="webpages:Enabled" value="false" />
18     <add key="ClientValidationEnabled" value="true" />
19     <add key="UnobtrusiveJavaScriptEnabled" value="true" />
20   </appSettings>
21   <system.web>
22     <authentication mode="None" />
23     <compilation debug="true" targetFramework="4.5" />
24     <httpRuntime targetFramework="4.5" />
25   </system.web>
26   <system.webServer>
27     <modules>
28       <remove name="FormsAuthenticationModule" />
29     </modules>
30   </system.webServer>
31   <runtime>
32     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
33       <dependentAssembly>
34         <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
35         <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
36       </dependentAssembly>
37       <dependentAssembly>
38         <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
39         <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
40       </dependentAssembly>
41       <dependentAssembly>
42         <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
43         <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
44       </dependentAssembly>
45       <dependentAssembly>
46         <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
47         <bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
48       </dependentAssembly>
49     </assemblyBinding>
50   </runtime>
51   <entityFramework>
52     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
53       <parameters>
54         <parameter value="v11.0" />
55       </parameters>
56     </defaultConnectionFactory>
57     <providers>
58       <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
59     </providers>
60   </entityFramework>
61 </configuration>

获取与修改appSettings配置节点下的属性值

WebConfigurationManager提供了AppSettings属性,便于访问配置文件中appSettings节点下的元素的value值

下图为Web.config中要获取值的元素

编写如下代码,并在第27行打上断点调试

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 using System.Web.Configuration;
 7 using System.Text;
 8 using System.Collections.Specialized;
 9 using System.Collections;
10 using System.Diagnostics;
11 namespace WebApplication1.Controllers
12 {
13     public class HomeController : Controller
14     {
15         public ActionResult Index()
16         {
17
18             string version = WebConfigurationManager.AppSettings["webpages:Version"];
19             string enabled = WebConfigurationManager.AppSettings["webpages:Enabled"];
20             string clientValidationEnabled = WebConfigurationManager.AppSettings["ClientValidationEnabled"];
21             string unobtrusiveJavaScriptEnabled = WebConfigurationManager.AppSettings["UnobtrusiveJavaScriptEnabled"];
22             Debug.WriteLine("配置信息如下");
23             Debug.WriteLine(version);
24             Debug.WriteLine(enabled);
25             Debug.WriteLine(clientValidationEnabled);
26             Debug.WriteLine(unobtrusiveJavaScriptEnabled);
27             return Content("");
28         }
29
30     }
31 }

调试模式“输出”窗口

以WebConfigurationManager.AppSettings["ClientValidationEnabled"]="要修改的值" 可直接修改值

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 using System.Web.Configuration;
 7 using System.Text;
 8 using System.Collections.Specialized;
 9 using System.Collections;
10 using System.Diagnostics;
11 namespace WebApplication1.Controllers
12 {
13     public class HomeController : Controller
14     {
15         public ActionResult Index()
16         {
17
18             string version = WebConfigurationManager.AppSettings["webpages:Version"];
19             string enabled = WebConfigurationManager.AppSettings["webpages:Enabled"];
20             WebConfigurationManager.AppSettings["ClientValidationEnabled"]="aaaaaaaaaaaaaaaaaaaaa";//动态修改value
21             string clientValidationEnabled = WebConfigurationManager.AppSettings["ClientValidationEnabled"];
22             string unobtrusiveJavaScriptEnabled = WebConfigurationManager.AppSettings["UnobtrusiveJavaScriptEnabled"];
23             Debug.WriteLine("配置信息如下");
24             Debug.WriteLine(version);
25             Debug.WriteLine(enabled);
26             Debug.WriteLine(clientValidationEnabled);
27             Debug.WriteLine(unobtrusiveJavaScriptEnabled);
28             return Content("");
29         }
30
31     }
32 }

调试输出

连接字符串修改

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

未完待续-----

转载于:https://www.cnblogs.com/chenghyi/p/5490056.html

C#操作项目配置文件相关推荐

  1. eclipse spring boot项目搭建_spring-boot-plus项目配置文件(四)

    spring-boot-plus项目配置文件 https://github.com/geekidea/spring-boot-plus https://github.com/geekidea/spri ...

  2. java tomcat 读取配置文件端口_跟我学Java编程—应用读写项目配置文件的Properties类...

    我们在开发软件项目时,经常需要读取项目的一些配置数据.例如,项目里用到的数据库链接地址.Tomcat服务端口号.数据文件备份地址等信息都需要放到一个配置文件里,由程序读取并获取.这样做的好处是,当需要 ...

  3. java生成world文件_Hello World 项目创建与项目配置文件介绍

    Hello World 项目创建 如上图标注 1 所示,点击 Create New Project 进入向导式创建项目 如上图标注 1 所示,如果此时 IntelliJ IDEA 还没有配置任何一个 ...

  4. ssm项目配置文件中的包扫描bean,排除特定bean的扫描

    ssm项目配置文件中的包扫描bean springMVC.xml: 自动扫描controller包下的所有类,使其认为spring mvc的控制器 <!-- 自动扫描controller包下的所 ...

  5. vue-cli+gitlab代码管理时,vue项目配置文件设置

    2019独角兽企业重金招聘Python工程师标准>>> vue-cli生成的项目,使用gitlab进行代码管理,怎样忽略项目配置文件的提交(避免每次合并分支代码冲突,而且每次修改配置 ...

  6. 微信小程序:全新强大的恋爱话术微信小程序源码土味情话视频号or自媒体操作项目

    你猜的不错,这就是一款恋爱话术小程序 该款小程序相对来说还是挺强大的 而且还融合了小编前段时间发布的一款土味情话在里面 这款小程序基本分段都是和外面几千块几百块的分段是一样的 如有分段是: 开场阶段丨 ...

  7. python的cfg是什么模块_python操作cfg配置文件方式

    *.cfg文件一般是程序运行的配置文件,python为读写常见配置文件提供了一个ConfigParser模块,所以在python中解析配置文件相当简单,下面就举例说明一下具体的操作方法. 写文件代码: ...

  8. pyyaml操作yaml配置文件基于python

    在测试工作中,可以使用yaml编写测试用例,执行测试用例时直接获取yaml中的用例数据进行测试(如:接口自动化测试) 1.什么是yaml 是一种可读的数据序列化语言,通常用于配置文件 非常简洁和强大, ...

  9. vue-cli的webpack模板项目配置文件分析[转]

    vue-cli的webpack模板项目配置文件分析[转] 原文出处:http://blog.csdn.net/hongchh/article/details/55113751 由于最近在vue-cli ...

最新文章

  1. Redis缓存穿透击穿雪崩
  2. 十六届全国大学生智能车竞赛线上比赛的队伍看过来,你们需要的图片都在这儿
  3. mac 查看mysql是否安装_[简明核心系列] 三分钟Mac安装MySQL教程
  4. Python剑指offer:和为s的连续整数序列
  5. GetDlgItem
  6. 史上最拉风年货?苏宁门店私人飞机开售 网友:这个真香不了吧
  7. android评论数据如何返回@用户_教你如何用JavaScript来驯服服务端返回的数据
  8. 【工具】 原版完美激活 Flash builder 4.7 【非破解激活】
  9. 天花板级软测项目拆分详解,年后涨薪面试,稳了...
  10. js打印线程id_Node.js多线程完全指南[每日前端夜话0x43]
  11. 神奇的margin之豆瓣豆瓣么么哒
  12. 百度网盘分享qt相关视频
  13. 【成功实践篇】VirtualBoX虚拟机和本地共享存储目录
  14. centos7编写shell批处理文件和执行方法
  15. 银河麒麟V10 远程桌面
  16. 怎样隐藏计算机中的文件夹,电脑上的文件夹不想被别人看到怎么办?如何隐藏电脑文件夹?-电脑文件夹怎么加密...
  17. SQL Server 非对称秘钥管理
  18. 为什么我从PR里面导出来的视频,在电脑上播放是正常的,微信发给朋友后,形状就变了,扭曲了一样的
  19. 阅读报告Maneuvering periods of 2D quantum walks with the coin operator
  20. 瑜伽健身app开发功能详细说明

热门文章

  1. 跨时代的传承者:天美如何用游戏重塑“敦煌印象”?
  2. 45岁码农用不到2年时间撸出100款扑克游戏
  3. 使用Workbench导出为【sql】脚本文件
  4. 程序员养生之道——坚持泡脚(十五分钟以上)
  5. expdp impdp中 exclude/include 的使用
  6. ZOJ-1101-Gamblers
  7. 使用CEfSharp之旅(8)CEFSharp 使用代理 更换位置IP
  8. unittest框架学习笔记
  9. Spring boot集成spring-boot-starter-data-jpa环境搭建
  10. [bzoj3131]淘金[sdoi2013][数位DP]