c# – 为3种不同的环境转换app.config

我需要能够使用msbuild转换我的app.config文件.我可以转换文件,如果它被称为app.DEBUG.config或app.Release.config,但我不能添加一个名为app.PROD.config文件.

使用常规XDT转换如果我选择不同的PublishProfile,msbuild会识别不同的web.config文件

msbuild path.to.project.csproj Configuration=Release PublishProfile=DEV

显然app.config不能使用相同的设置.我总是可以为DEV.config设置创建一个特定的构建配置,但对一个应用程序进行单独的构建确认似乎没用.一个hacky方法是只为每个环境POST-BUILD复制正确的app.config.

我试图使用SlowCheetah插件,但这似乎只是转换认的DEBUG和RELEASE配置文件,这是不合适的,因为我有两个以上的环境.如果我确实使用了这个错误,请让我知道我应该将哪个参数传递给msbuild来选择我的app.DEV.config.

预期的结果是msbuild将根据自定义的变换app.DEV.config或为app.PROD.config自定义的变换来变换app.config.我希望有一个参数可以传递给msbuild,这将允许我使用Release配置,但每个环境使用不同名称的转换.

解决方法

这是我在这种情况下使用的:
<?xml version="1.0" encoding="utf-8"?>
<Project Toolsversion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <!-- This target will run right before you run your app in Visual Studio -->
  <Target Name="UpdateWebConfigBeforeRun" BeforeTargets="Build">
    <Message Text="Configuration: $(Configuration) update from web.template.$(Configuration).config"/>
    <TransformXml Source="web.template.config"
              Transform="web.template.$(Configuration).config"
              Destination="web.config" />
    </Target>

  <!-- Exclude the config template files from the created package -->
  <Target Name="ExcludeCustomConfigTransformFiles" BeforeTargets="ExcludeFilesFromPackage">
    <ItemGroup>
      <ExcludeFromPackageFiles Include="web.template.config;web.template.*.config"/>
    </ItemGroup>
    <Message Text="ExcludeFromPackageFiles: @(ExcludeFromPackageFiles)" Importance="high"/>
  </Target>
</Project>

我有以下设置:

web.template.config
    - web.template.debug.config
    - web.template.production.config
    - web.template.release.config etc

应该在不需要额外插件的情况下工作.在你的场景中,你需要编辑内容来说app.而不是网络.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...