Console Program app.config trasform
I have a console app to some of the background tasks.I got some troubles with switch QAS stage and production stage.For that, I need to replace the app setting for difference machine.Therefore I write some codes to do thus to got my purpose.Today I saw an article to introduce to do that.I realize that I have done the stupid thing to deal with that thing.This article is for my regretted note.
![image image](https://lh3.googleusercontent.com/-sPW3SOnneHU/WK-NdcHY3jI/AAAAAAACJWI/vW-jzBUwEV4/image_thumb%25255B1%25255D.png?imgmax=800)
add two files: App.Release.config and App.Debug.config
![image image](https://lh3.googleusercontent.com/-i8FLjmXBNm8/WK-Nl1yQ85I/AAAAAAACJWQ/fNfw0vl8mWQ/image_thumb.png?imgmax=800)
in App.Release.config add Stage setting,value=Release
![image image](https://lh3.googleusercontent.com/-x7lfhINmfk8/WK-NtwiGofI/AAAAAAACJWY/VtqYCnqTMzs/image_thumb1.png?imgmax=800)
Debug.config keep empty,the file is required for transform checking if exists.
Reference as below image
![image image](https://lh3.googleusercontent.com/-cXk-D148MF4/WK-N18Zz-cI/AAAAAAACJWg/n_iGmWgONRU/image_thumb3.png?imgmax=800)
after saving,these files will be presenting by the hierarchy
![image image](https://lh3.googleusercontent.com/-Z6aT-8OGENQ/WK-N8GaeTeI/AAAAAAACJWo/5z_GEmbWYnQ/image_thumb4.png?imgmax=800)
![image image](https://lh3.googleusercontent.com/-2b_FA-QcNY8/WK-OCnpBObI/AAAAAAACJW0/jFWSXTdx7po/image_thumb5.png?imgmax=800)
In App.config add connectionStrings setting
![image image](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi31hQMcavR9biSyiGwYktkLirYzFUix_R7HVRQpj-Srlqugmq2c4iQFYTYk_FwPgtiULVobcAX3ckSpp89kal3rJsxb-yCHqSy_5joxipXHw39YH7c0l-HpXlY-LZ9aau1bP-Z5AEV_jQ/?imgmax=800)
add file "Connection.config"
![image image](https://lh3.googleusercontent.com/-u8DCTlIKw8k/WK-OQi_ngZI/AAAAAAACJXE/X6svnAhR6QM/image_thumb%25255B3%25255D.png?imgmax=800)
Connection.config file property set "Copy always"
![image image](https://lh3.googleusercontent.com/-V_59Ixtgyd4/WK-OYPvDwvI/AAAAAAACJXQ/j5ElACBDEUg/image_thumb6.png?imgmax=800)
App.Release.config set connectionStrings point to Connection.config
![image image](https://lh3.googleusercontent.com/-RO51ApRkTRA/WK-Oe0pzASI/AAAAAAACJXY/PMudzQXSj9M/image_thumb%25255B4%25255D.png?imgmax=800)
![image image](https://lh3.googleusercontent.com/-JpGatFqBfsI/WK-OlbXlXbI/AAAAAAACJXg/BnEnrqaKDIs/image_thumb%25255B5%25255D.png?imgmax=800)
Source Code: https://github.com/kimx/AppConfigTransformLab
https://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx
Auto transform App.config
App.config add Stage setting,value=Debug![image image](https://lh3.googleusercontent.com/-sPW3SOnneHU/WK-NdcHY3jI/AAAAAAACJWI/vW-jzBUwEV4/image_thumb%25255B1%25255D.png?imgmax=800)
add two files: App.Release.config and App.Debug.config
![image image](https://lh3.googleusercontent.com/-i8FLjmXBNm8/WK-Nl1yQ85I/AAAAAAACJWQ/fNfw0vl8mWQ/image_thumb.png?imgmax=800)
in App.Release.config add Stage setting,value=Release
![image image](https://lh3.googleusercontent.com/-x7lfhINmfk8/WK-NtwiGofI/AAAAAAACJWY/VtqYCnqTMzs/image_thumb1.png?imgmax=800)
Debug.config keep empty,the file is required for transform checking if exists.
project.csproj add msbuild settings as below
1.PropertyGroup2.ItemGroup<PropertyGroup> <ProjectConfigFileName>App.config</ProjectConfigFileName> </PropertyGroup>
3.Import<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>
<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>
Reference as below image
![image image](https://lh3.googleusercontent.com/-cXk-D148MF4/WK-N18Zz-cI/AAAAAAACJWg/n_iGmWgONRU/image_thumb3.png?imgmax=800)
after saving,these files will be presenting by the hierarchy
![image image](https://lh3.googleusercontent.com/-Z6aT-8OGENQ/WK-N8GaeTeI/AAAAAAACJWo/5z_GEmbWYnQ/image_thumb4.png?imgmax=800)
Testing Build
To switch Release then compile,the value of Stage has changed "Release"![image image](https://lh3.googleusercontent.com/-2b_FA-QcNY8/WK-OCnpBObI/AAAAAAACJW0/jFWSXTdx7po/image_thumb5.png?imgmax=800)
transform connection string with the outer file
In App.config add connectionStrings setting
add file "Connection.config"
![image image](https://lh3.googleusercontent.com/-u8DCTlIKw8k/WK-OQi_ngZI/AAAAAAACJXE/X6svnAhR6QM/image_thumb%25255B3%25255D.png?imgmax=800)
Connection.config file property set "Copy always"
![image image](https://lh3.googleusercontent.com/-V_59Ixtgyd4/WK-OYPvDwvI/AAAAAAACJXQ/j5ElACBDEUg/image_thumb6.png?imgmax=800)
App.Release.config set connectionStrings point to Connection.config
![image image](https://lh3.googleusercontent.com/-RO51ApRkTRA/WK-Oe0pzASI/AAAAAAACJXY/PMudzQXSj9M/image_thumb%25255B4%25255D.png?imgmax=800)
Testing Build
To switch Release then compile.the configSource has referred to outer file.![image image](https://lh3.googleusercontent.com/-JpGatFqBfsI/WK-OlbXlXbI/AAAAAAACJXg/BnEnrqaKDIs/image_thumb%25255B5%25255D.png?imgmax=800)
Source Code: https://github.com/kimx/AppConfigTransformLab
References
http://demo.tc/post/775https://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx