app.config
在写WPF的时候配置文件app.config 读取的办法 -- 【可以直接安装 Microsoft.VisualStudio.SlowCheetah.vsix】
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="abc" value="123"/> </appSettings> </configuration>
读取的方法
System.Configuration.ConfigurationManager.AppSettings["abc"];
这本是没有什么可记录的,但是想debug 和release分开为 app.Debug.config 和app.Release.config两个文件,
<!-- Debug 配置 --> <PropertyGroup Condition="'$(Configuration)' == 'Debug'"> <DefineConstants>DEBUG;TRACE</DefineConstants> <AppConfigFile>app.debug.config</AppConfigFile> </PropertyGroup> <!-- Release 配置 --> <PropertyGroup Condition="'$(Configuration)' == 'Release'"> <DefineConstants>TRACE</DefineConstants> <AppConfigFile>app.config</AppConfigFile> </PropertyGroup> <!-- 自动切换配置文件 --> <Target Name="TransformAppConfig" BeforeTargets="BeforeBuild"> <Copy SourceFiles="$(AppConfigFile)" DestinationFiles="app.config" /> </Target>
但是这样一来 有个问题,需要两份,相同的东西也需要放两边,需要同时修改两分,需要搞一个共有的一份 放app.config 需要安装Microsoft.VisualStudio.SlowCheetah的引用
<ItemGroup> <None Update="App.config"> <TransformOnBuild>true</TransformOnBuild> </None> <None Update="App.Debug.config"> <IsTransformFile>true</IsTransformFile> <DependentUpon>App.config</DependentUpon> </None> <None Update="App.Release.config"> <IsTransformFile>true</IsTransformFile> <DependentUpon>App.config</DependentUpon> </None> </ItemGroup>
最后注意对比 三个config的区别
<!-- app.config --!>
<?xml version="1.0" encoding="utf-8"?>
<configuration > <appSettings> <add key="abc" value="123" /> <add key="share" value="share" /> </appSettings> </configuration>
<!-- app.Debug.config --!>
<?xml version="1.0" encoding="utf-8" ?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <appSettings> <add key="abc" value="debug123" xdt:Transform="Replace" xdt:Locator="Match(key)"/> <add key="share" value="debug" xdt:Transform="Replace" xdt:Locator="Match(key)"/> </appSettings> </configuration>
<!-- app.Release.config --!>
<?xml version="1.0" encoding="utf-8" ?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <appSettings> <add key="share" value="Release" xdt:Transform="Replace" xdt:Locator="Match(key)"/> </appSettings> </configuration>