【问题标题】:app.config file in the new .csproj format新的 .csproj 格式的 app.config 文件
【发布时间】:2020-05-14 12:05:24
【问题描述】:

Visual Studio 提供了精简的 .csproj 文件格式。

我正在尝试构建一个 .NET Framework 应用程序(版本 4.6.1),但使用新的可手动编辑的文件。

与之前的行为类似,我希望将 app.config 文件复制到输出目录,但重命名为 <output_exe>.config(其中 output_exe 是可执行文件的名称)。

我们在 .csproj 文件中放置了什么来实现这一点?

这不起作用,因为它不会重命名文件:

<ItemGroup>
  <Content Include="App.config">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
</ItemGroup>

【问题讨论】:

  • 您必须创建一个自定义目标或调用 post-build-event 为您重命名文件...

标签: visual-studio msbuild csproj


【解决方案1】:

只需添加 msbuild 和工具对此功能所期望的 AppConfig 属性:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net472</TargetFramework>
    <AppConfig>App.config</AppConfig>
  </PropertyGroup>

</Project>

这使PrepareForBuild msbuild 目标能够自动获取文件,并且后续构建步骤也可以将此文件编辑为逻辑文件 - 例如SDK 将根据定义的TargetFramework 修改 startup/supportedRuntime 部分。将此添加为自定义项目或构建步骤将失去此功能。

【讨论】:

    【解决方案2】:

    尝试添加如下脚本来完成这项工作:

      <ItemGroup>
        <Content Include="App.config">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
      </ItemGroup>
    

      <Target Name="MyCustomTarget" AfterTargets="build">
        <Move SourceFiles="$(OutputPath)\App.config" DestinationFiles="$(OutputPath)\$(AssemblyName).config" />
      </Target>
    

    第一个脚本可以确保App.config 文件将被复制到输出文件夹,第二个脚本将在构建目标后将文件重命名为 AssemblyName(可执行文件的名称)。

    更多细节:如果我们需要 ProjectName.exe.config,请使用$(OutputPath)\$(AssemblyName).exe.config。如果我们需要 ProjectName.config,则使用$(OutputPath)\$(AssemblyName).config。我们可以根据需要自定义重命名文件的格式。

    【讨论】:

    • 我认为你必须明确说明文件扩展名:即$(OutputPath)\$(AssemblyName).exe.config
    • @AndrewShepherd 是的,假设我们有一个 Test.exe。如果需要Test.config,则使用$(OutputPath)\$(AssemblyName).config,如果需要Test.exe.config,则使用$(OutputPath)\$(AssemblyName).exe.config,我们可以自定义格式得到我们需要的。
    猜你喜欢
    • 1970-01-01
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 2019-06-26
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    相关资源
    最近更新 更多