简体   繁体   English

app.config名称是硬编码的还是可以修改的?

[英]Is app.config name hard-coded or can it be modified?

I know that when building in VS, app.config gets copied to <configuration>/<app-name>.exe.config . 我知道在VS中构建时, app.config被复制到<configuration>/<app-name>.exe.config I believe this copy step is performed by msbuild, which VS uses for building? 我相信此复制步骤是由msbuild执行的,哪个VS用于构建?

I've seen some suggestions that this can be customised through build settings or properties or something, but I can't find a definitive answer. 我已经看到一些建议,可以通过构建设置或属性或某些内容对其进行自定义,但是我找不到确切的答案。

My two linked questions are: 我的两个链接问题是:

  1. Who/what copies the app.config file to the output directory during building? 谁/在构建期间将app.config文件复制到输出目录? And at what point in the build? 在构建的什么时候?
  2. Can I control the name of the file that is searched for eg change it to Jeff.config or {ConfigurationName}.cfg ? 我可以控制搜索文件的名称吗,例如将其更改为Jeff.config{ConfigurationName}.cfg

Technically yes, but you may need to be mindful as you're getting deeper in the pipeline. 从技术上讲是可以的,但是随着您对管道的深入了解,您可能需要注意。 If you'll check this file: 如果您要检查此文件:

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

You can find couple of interesting targets: 您可以找到几个有趣的目标:

PrepareForBuild: Prepare the prerequisites for building. 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>

which builds up AppConfigWithTargetPath ItemGroup (and this itemgroup is used in many other targets). 这将建立AppConfigWithTargetPath ItemGroup(该itemgroup用于许多其他目标)。 And another target, which performs the actual copying - _CopyAppConfigFile :Copy the application config file. 另一个执行实际复制的目标_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>

Knowing how it's being copied - you can affect what should be copied. 了解如何复制-您可以影响应复制的内容。 Most clean way is to define\\overwrite a value in $(AppConfig) property - then everything will have to use this new specified config. 最干净的方法是在$(AppConfig)属性中定义\\覆盖一个值-然后一切都必须使用此新的指定配置。

Depends on how you build things - you may need to create pre-solution file to hook in or modify explicitly your build script to pass extra parameters. 取决于您构建事物的方式-您可能需要创建解决方案文件以挂接或显式修改构建脚本以传递额外的参数。 Or you may search in your csproj files and edit file name directly there. 或者,您可以搜索csproj文件并直接在其中编辑文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM