主控台應用程式的組態置換
自動切換App.config
在App.config 新增Stage 設定,值=Debug
新增App.Release.config及App.Debug.config

在App.Release.config 新增Stage 設定,值=Release

Debug.config 保持空值,此檔案為必要檔案,因待會的轉換時以檢查此檔案是否存在。
在專案檔.csproj 加入如下的建置設定
1.PropertyGroup<PropertyGroup>
<ProjectConfigFileName>App.config</ProjectConfigFileName>
</PropertyGroup>
2.ItemGroup<ItemGroup>
<None Include="App.config" />
<None Include="App.Debug.config">
<DependentUpon>App.config</DependentUpon>
</None>
<None Include="App.Release.config">
<DependentUpon>App.config</DependentUpon>
</None>
</ItemGroup>
3.Import <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.Publishing.targets" />
<Target Name="AfterBuild">
<TransformXml Source="@(AppConfigWithTargetPath)" Transform="$(ProjectConfigTransformFileName)"
Destination="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" />
</Target>
參照如下圖

儲存後,此時檔案結構會變成有階層順序

測試建置
切換到Release建置後,此時Stage 的值已更改為Release
同場加映切換連線字串為外部檔案
在App.config 新增connectionStrings設定

新增Connection.config,如下

Connection.config設定輸出屬性為Copy always

在App.Release.config 將connectionStrings指向Connection.config

測試建置
切換到Release建置後,此時connection 已指向外部檔案
原始檔 https://github.com/kimx/AppConfigTransformLab
參考文章
http://demo.tc/post/775https://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx