Visual Studio:区分调试和发布模式的 app.config

发布于 2024-08-02 09:21:17 字数 197 浏览 4 评论 0原文

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

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

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

Is there a way to automatically use a separate app.config when building in release mode?

In other words, I want to test with one app.config, and release with another.

Currently, I keep a separate copy called app.config.production, and manually overwrite bin\Release\Application.exe.config after building for release.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

肩上的翅膀 2024-08-09 09:21:17

通过上下文菜单在解决方案资源管理器中卸载项目。

通过上下文菜单编辑 .csproj 文件并添加以下内容:

<PropertyGroup>
    <AppConfig>App.$(Configuration).config</AppConfig>
</PropertyGroup>

Unload the project in Solution Explorer via the context menu.

Edit the .csproj file via the context menu and add this:

<PropertyGroup>
    <AppConfig>App.$(Configuration).config</AppConfig>
</PropertyGroup>
薆情海 2024-08-09 09:21:17

一个干净的解决方案是将 2 个文件 App.Debug.configApp.Release.config 分组到 App.config 中,并将好的文件更改为App.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>

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

screenshot

A clean solution is to group 2 files App.Debug.config and App.Release.config into App.config and change the good file into App.config depending on the configuration at compile time:

<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>

With this solution you will get something like this in Visual Studio:

screenshot

瀟灑尐姊 2024-08-09 09:21:17

一种简单快速的方法是创建第二个文件“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"

以及此构建后事件:

IF $(ConfigurationName) == Release COPY /Y "$(ProjectDir)App.debug.config" "$(ProjectDir)App.config"

这可能有点奇怪,但它允许您继续使用 .Settings 文件作为调试设置,仍然链接到 App.configApp.release.config 必须手动构建,但切换此功能非常容易。

A simple and fast way is to create a second file "App.release.config" and insert this pre-build event:

IF $(ConfigurationName) == Release COPY /Y "$(ProjectDir)App.config" "$(ProjectDir)App.debug.config"
IF $(ConfigurationName) == Release COPY /Y "$(ProjectDir)App.release.config" "$(ProjectDir)App.config"

And this post build event:

IF $(ConfigurationName) == Release COPY /Y "$(ProjectDir)App.debug.config" "$(ProjectDir)App.config"

This might be a bit odd, but it will allow you to keep using the .Settings files as debug settings, that are still linked to the App.config. The App.release.config must be build by hand, but it's pretty easy to switch this functionality.

韵柒 2024-08-09 09:21:17

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

I highly recommend SlowCheetah for app.config transformations. Visit this nuget gem here Visual Studio Gallery

时光是把杀猪刀 2024-08-09 09:21:17

与最佳答案类似,但通过这种方法,如果愿意,您可以看到实际文件,并且智能感知不会在 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>

Similar to top answer but with this approach you can see the actual file if preferred and intellisense doesn't complain in csproj file:

  <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>
可可 2024-08-09 09:21:17

我不知道这是否有帮助,但 app.config 将识别标准 MSBUILD 替换字符串,例如 $(Configuration)。

I don't know if this helps, but app.config will recognise the standard MSBUILD substitution strings such as $(Configuration).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文