对于Visual Studio 2010基于Web的应用程序,咱们具备Config Transformation功能,经过该功能,咱们能够为不一样的环境维护多个配置文件。 可是,对于Windows Services / WinForms或控制台应用程序,App.Config文件没法使用相同的功能。 web
这里提供了一种解决方法: 将XDT魔术应用于App.Config 。 app
然而,它并不简单,须要许多步骤。 有没有更简单的方法来实现相同的app.config文件? 编辑器
我编写了一个很好的扩展来自动化app.config转换,就像Web应用程序项目配置转换中内置的同样 ide
此扩展的最大优势是您无需在全部构建计算机上安装它 visual-studio
您能够为每一个配置使用单独的配置文件,例如app.Debug.config,app.Release.config,而后在项目文件中使用配置变量: 测试
<PropertyGroup> <AppConfig>App.$(Configuration).config</AppConfig> </PropertyGroup>
而后,这将根据您正在构建的配置建立正确的ProjectName.exe.config文件。 ui
对如今彷佛处处发布的解决方案稍做改进: this
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
受Oleg和其余人在这个问题的启发,我进一步采用了解决方案https://stackoverflow.com/a/5109530/2286801以启用如下功能。 spa
此解决方案经过在MSBuild进程中首次引用app.config以前执行app.config转换来工做。 它使用外部目标文件,以便更轻松地管理多个项目。 调试
与其余解决方案相似的步骤。 我引用了相同的内容并将其包含在内,以便完整性和更容易比较。
0.在项目中添加一个名为AppConfigTransformation.targets的新文件
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <!-- Transform the app config per project configuration.--> <PropertyGroup> <!-- This ensures compatibility across multiple versions of Visual Studio when using a solution file. However, when using MSBuild directly you may need to override this property to 11.0 or 12.0 accordingly as part of the MSBuild script, ie /p:VisualStudioVersion=11.0; See http://blogs.msdn.com/b/webdev/archive/2012/08/22/visual-studio-project-compatability-and-visualstudioversion.aspx --> <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion> </PropertyGroup> <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.targets" /> <Target Name="SetTransformAppConfigDestination" BeforeTargets="PrepareForBuild" Condition="exists('app.$(Configuration).config')"> <PropertyGroup> <!-- Force build process to use the transformed configuration file from now on. --> <AppConfig>$(IntermediateOutputPath)$(TargetFileName).config</AppConfig> </PropertyGroup> <Message Text="AppConfig transformation destination: = $(AppConfig)" /> </Target> <!-- Transform the app.config after the prepare for build completes. --> <Target Name="TransformAppConfig" AfterTargets="PrepareForBuild" Condition="exists('app.$(Configuration).config')"> <!-- Generate transformed app config in the intermediate directory --> <TransformXml Source="app.config" Destination="$(AppConfig)" Transform="app.$(Configuration).config" /> </Target> </Project>
1.为项目添加每一个配置的XML文件。
一般,您将具备调试和发布配置,所以将文件命名为App.Debug.config和App.Release.config。 在个人项目中,我为每种环境建立了一个配置,所以您可能想要尝试一下。
2.卸载项目并打开.csproj文件进行编辑
Visual Studio容许您在编辑器中编辑.csproj - 您只须要先卸载项目。 而后右键单击它并选择Edit .csproj。
3.将App。*。配置文件绑定到主App.config
找到包含全部App.config和App。*。config引用的项目文件部分,并替换以下。 您会注意到咱们使用None而不是Content。
<ItemGroup> <None Include="app.config"/> <None Include="app.Production.config"> <DependentUpon>app.config</DependentUpon> </None> <None Include="app.QA.config"> <DependentUpon>app.config</DependentUpon> </None> <None Include="app.Development.config"> <DependentUpon>app.config</DependentUpon> </None> </ItemGroup>
4.激活变形魔法
在文件结束以后
<Import Project="$(MSBuildToolsPath)\\Microsoft.CSharp.targets" />在决赛以前
</Project>
插入如下XML:
<Import Project="AppConfigTransformation.targets" />
完成!
因此我最终采起了略微不一样的方法。 我按照Dan的步骤完成了第3步,但添加了另外一个文件:App.Base.Config。 此文件包含每一个生成的App.Config中所需的配置设置。 而后我使用BeforeBuild(将Yuri添加到TransformXml中)将当前配置与Base配置转换为App.config。 而后,构建过程正常使用转换后的App.config。 可是,有一种烦恼是你想要从源代码控制中排除不断变化的App.config,但其余配置文件如今依赖于它。
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" /> <Target Name="BeforeBuild" Condition="exists('app.$(Configuration).config')"> <TransformXml Source="App.Base.config" Transform="App.$(Configuration).config" Destination="App.config" /> </Target>