visual-studio - Visual Studio 中非 Web 项目的项目的 App.Config 转换?

标签 visual-studio .net-4.0 app-config slowcheetah web-config-transform

对于基于 Visual Studio 2010 Web 的应用程序,我们具有配置转换功能,通过该功能我们可以为不同的环境维护多个配置文件。但相同的功能不适用于 Windows 服务/WinForms 或控制台应用程序的 App.Config 文件。

这里建议有一个解决方法:Applying XDT magic to App.Config .

然而,这并不简单,需要执行许多步骤。有没有更简单的方法可以对 app.config 文件实现相同的效果?

最佳答案

我尝试了几种解决方案,这是我个人发现的最简单的解决方案。
Dan评论中指出the original post属于Oleg Sych -谢谢,奥列格!

以下是说明:

<强>1。将每个配置的 XML 文件添加到项目中。

通常,您将拥有 DebugRelease 配置,因此将文件命名为 App.Debug.configApp.Release.config 。在我的项目中,我为每种环境创建了一个配置,因此您可能想尝试一下。

<强>2。卸载项目并打开.csproj文件进行编辑

Visual Studio 允许您直接在编辑器中编辑 .csproj 文件 - 您只需先卸载项目即可。然后右键单击它并选择编辑 .csproj

<强>3。将 App.*.config 文件绑定(bind)到主 App.config

找到包含所有 App.configApp.*.config 引用的项目文件部分。您会注意到他们的构建操作设置为None,没关系:

<None Include="App.config" />
<None Include="App.Debug.config" />
<None Include="App.Release.config" />

接下来,使所有配置特定文件依赖于主App.config,以便 Visual Studio 将它们分组,就像设计器和代码一样- 后面的文件。

将上面的 XML 替换为下面的 XML:

<None Include="App.config" />
<None Include="App.Debug.config" >
  <DependentUpon>App.config</DependentUpon>
</None>
<None Include="App.Release.config" >
  <DependentUpon>App.config</DependentUpon>
</None>

<强>4。激活转换魔法(对于 VS2019 等 Visual Studio 版本仍然必需)

在文件末尾之后

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

决赛前

</Project>

插入以下 XML - 请注意要进行正确的转换需要两个步骤:

  <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
  <Target Name="BeforeBuild" Condition="Exists('App.$(Configuration).config')">
    <!-- Generate transformed app config and replace it: will get the <runtime> node and assembly bindings properly populated -->
    <TransformXml Source="App.config" Destination="App.config" Transform="App.$(Configuration).config" />
  </Target>
  <Target Name="AfterBuild" Condition="Exists('App.$(Configuration).config')">
    <!-- Generate transformed app config in the intermediate directory: this will transform sections such as appSettings -->
    <TransformXml Source="App.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="App.$(Configuration).config" />
    <!-- Force build process to use the transformed configuration file from now on.-->
    <ItemGroup>
      <AppConfigWithTargetPath Remove="App.config" />
      <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
        <TargetPath>$(TargetFileName).config</TargetPath>
      </AppConfigWithTargetPath>
    </ItemGroup>
  </Target>

现在您可以重新加载项目,构建它并享受 App.config 转换!

仅供引用

确保您的 App.*.config 文件具有如下正确设置:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
     <!--magic transformations here-->
</configuration>

关于visual-studio - Visual Studio 中非 Web 项目的项目的 App.Config 转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3004210/

相关文章:

Visual Studio Express 2015 中的 C++ 新文件问题

c# - Web API 性能?

c# - app.config 中的嵌套自定义元素 (C#)

c# - 大型 c# 解决方案中的配置文件

asp.net - Visual Studio 2010 SP1 中的 .ASPX 文件不会启动自动大纲(可折叠部分)

c# - 在调试期间,进入后台工作线程/线程。可能的?

html - 为什么 Live Server 不从 Visual Studio 预览我的 CSS?

.net - 何时以及为什么需要supportedRuntime 元素和sku 属性?

c# - 事件触发事件

c# - 使用 XmlDocument 读取 app.config