Visual Studio:区分app.config以进行调试和发布模式

Kor*_*rey 35 app-config production-environment visual-studio

在发布模式下构建时,有没有办法自动使用单独的app.config?

换句话说,我想用一个app.config进行测试,然后用另一个进行测试.

目前,我保留了一个名为app.config.production的单独副本,并在构建发布后手动覆盖bin\Release\Application.exe.config.

Mat*_*u H 19

一个干净的解决方案是将 2 个文件分组为App.Debug.config,并根据编译时的配置将好的文件更改为:App.Release.configApp.configApp.config

<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>
<Target Name="SetAppConfig" BeforeTargets="Compile">
    <Copy SourceFiles="App.Debug.config" DestinationFiles="App.config" OverwriteReadOnlyFiles="true" Condition=" '$(Configuration)' == 'Debug' " />
    <Copy SourceFiles="App.Release.config" DestinationFiles="App.config" OverwriteReadOnlyFiles="true" Condition=" '$(Configuration)' == 'Release' " />
</Target>
Run Code Online (Sandbox Code Playgroud)

使用此解决方案,您将在 Visual Studio 中得到类似以下内容:

截屏


tru*_*oda 16

通过contet菜单卸载解决方案资源管理器中的项目.编辑.csproj文件.将此字符串添加到文件中.

<PropertyGroup>
    <AppConfig>App.$(Configuration).config</AppConfig>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)


ne1*_*10s 15

我最近发布了对类似SO主题的极其迟来的回应:https: //stackoverflow.com/a/27546685/2798367

为清楚起见,我将在此重复一遍:

这对派对来说有些晚了,但我偶然发现了一种实现文件web.transform方法的好方法app.config.(即它使用命名空间http://schemas.microsoft.com/XML-Document-Transform)

我认为这很"好",因为它是纯xml方法,不需要第三方软件.

根据您的各种构建配置,父/默认App.config文件来自.这些后代只会覆盖他们需要的东西.在我看来,这比必须保持x完整复制的配置文件数量要复杂得多,比如在其他答案中.

这里发布了一个演练:http://mitasoft.wordpress.com/2011/09/28/multipleappconfig/


看,妈妈 - 我的IDE中没有明确的构建后事件!

  • 对于VS 2017,`v10.0`必须在`<Import Project ="$(MSBuildExtensionsPath)\ Microsoft\VisualStudio\v15.0\Web\Microsoft.Web.Publishing.targets"中替换为`v15.0`"/ >在上面的演练中. (2认同)

mod*_*diX 7

一种简单快捷的方法是创建第二个文件"App.release.config"并插入此预构建事件:

IF $(ConfigurationName) == Release COPY /Y "$(ProjectDir)App.config" "$(ProjectDir)App.debug.config"
IF $(ConfigurationName) == Release COPY /Y "$(ProjectDir)App.release.config" "$(ProjectDir)App.config"
Run Code Online (Sandbox Code Playgroud)

这个帖子构建事件:

IF $(ConfigurationName) == Release COPY /Y "$(ProjectDir)App.debug.config" "$(ProjectDir)App.config"
Run Code Online (Sandbox Code Playgroud)

这可能有点奇怪,但它将允许您继续使用.Settings文件作为调试设置,仍然链接到App.config.的App.release.config必须是手工打造的,但它很容易切换此功能.

  • @Adrian右键单击解决方案中的项目,单击"属性",然后打开"构建事件" (2认同)

Rud*_*osa 7

我强烈推荐 SlowCheetah 用于 app.config 转换。在此处访问此 nuget gem Visual Studio Gallery

  • 好的!我希望这种变化很快就会出现。 (2认同)

Mar*_*sky 5

与最佳答案类似,但通过这种方法,如果愿意,您可以看到实际文件,并且智能感知不会在 csproj 文件中抱怨:

  <Target Name="SetAppConfig" BeforeTargets="Compile">
    <Copy SourceFiles="debug.config" DestinationFiles="app.config" OverwriteReadOnlyFiles="true" Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' " />
    <Copy SourceFiles="release.config" DestinationFiles="app.config" OverwriteReadOnlyFiles="true" Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' " />
  </Target>
Run Code Online (Sandbox Code Playgroud)