【问题标题】:Is app.config name hard-coded or can it be modified?app.config 名称是硬编码的还是可以修改的?
【发布时间】:2016-11-04 00:23:24
【问题描述】:

我知道在 VS 中构建时,app.config 会被复制到 <configuration>/<app-name>.exe.config。我相信这个复制步骤是由 VS 用于构建的 msbuild 执行的?

我看到了一些建议,可以通过构建设置或属性等来自定义,但我找不到明确的答案。

我的两个相关问题是:

  1. 在构建期间谁/什么将app.config 文件复制到输出目录?在构建的哪个阶段?
  2. 我可以控制搜索文件的名称吗?将其更改为Jeff.config 或{ConfigurationName}.cfg?

【问题讨论】:

    标签: .net visual-studio visual-studio-2012 msbuild app-config


    【解决方案1】:

    从技术上讲是的,但您可能需要注意,因为您正在深入研究管道。如果您要检查此文件:

    Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.targets

    您可以找到几个有趣的目标:

    PrepareForBuild:准备构建的先决条件。

      <Target Name="PrepareForBuild" DependsOnTargets="$(PrepareForBuildDependsOn)">
        <ItemGroup>
          <AppConfigWithTargetPath Include="$(AppConfig)" Condition="'$(AppConfig)'!=''">
            <TargetPath>$(TargetFileName).config</TargetPath>
          </AppConfigWithTargetPath>
        </ItemGroup>
    
        <FindAppConfigFile PrimaryList="@(None)" SecondaryList="@(Content)" TargetPath="$(TargetFileName).config" Condition="'$(AppConfig)'==''">
          <Output TaskParameter="AppConfigFile" ItemName="AppConfigWithTargetPath"/>
          <Output TaskParameter="AppConfigFile" PropertyName="AppConfig"/>
        </FindAppConfigFile>
    
        <!-- Create the directories for intermediate and final build products, and any other arbitrary directories. -->
        <!-- We are going to continue on error here so that if the tree is read only we will still get intellisense -->
        <MakeDir Directories="$(OutDir);$(IntermediateOutputPath);@(DocFileItem->'%(RelativeDir)');@(CreateDirectory)" ContinueOnError="True"/>
    
      </Target>
    

    它建立了AppConfigWithTargetPath ItemGroup(并且此 itemgroup 用于许多其他目标)。 还有另一个执行实际复制的目标 - _CopyAppConfigFile:复制应用程序配置文件。

      <Target
          Name="_CopyAppConfigFile"
          Condition=" '@(AppConfigWithTargetPath)' != '' "
          Inputs="@(AppConfigWithTargetPath)"
          Outputs="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')">
    
        <!--
            Copy the application's .config file, if any.
            Not using SkipUnchangedFiles="true" because the application may want to change the app.config and not have an incremental build replace it.
            -->
        <Copy
            SourceFiles="@(AppConfigWithTargetPath)"
            DestinationFiles="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')"
            OverwriteReadOnlyFiles="$(OverwriteReadOnlyFiles)"
            Retries="$(CopyRetryCount)"
            RetryDelayMilliseconds="$(CopyRetryDelayMilliseconds)"             
          UseHardlinksIfPossible="$(CreateHardLinksForAdditionalFilesIfPossible)" >
          <Output TaskParameter="DestinationFiles" ItemName="FileWrites"/>
        </Copy>
      </Target>
    

    了解它是如何被复制的 - 您可以影响应该复制的内容。 最干净的方法是在 $(AppConfig) 属性中定义\覆盖一个值 - 然后一切都必须使用这个新的指定配置。

    取决于您如何构建事物 - 您可能需要创建预解决方案文件以挂钩或显式修改您的构建脚本以传递额外参数。或者您可以在您的 csproj 文件中搜索并直接在那里编辑文件名。

    【讨论】:

    • 很有趣,谢谢。我原以为我可以简单地添加一个项目预构建事件copy $(ProjectDir)App.$(ConfigurationName).config $(ProjectDir)App.config,但显然这不起作用...我假设它发生在您引用的内容之后?
    • 您也可以这样做,但您提出了一些不同的问题;)。我为这种配置复制所做的最简单的技巧是连接一个命令,用特定的“Jeff.config”覆盖 app.config。但我只是解释了一些关于管道是如何工作的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 2020-08-03
    • 1970-01-01
    • 2011-01-21
    相关资源
    最近更新 更多