C#操作项目配置文件

时间:2023-03-08 17:26:25

前言

对于项目配置文件的读取和修改,.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=&quot;data source=.;initial catalog=Mooc;user id=sa;password=aaaaaa;MultipleActiveResultSets=True;App=EntityFramework&quot;" 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中要获取值的元素

C#操作项目配置文件

编写如下代码,并在第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("");
} }
}

调试模式“输出”窗口

C#操作项目配置文件

以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("");
} }
}

调试输出

C#操作项目配置文件

连接字符串修改

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

C#操作项目配置文件

未完待续-----