簡體   English   中英

Visual Studio / MSBuild將引用類庫的app.config復制為* .dll.config到當前項目的bin文件夾

[英]Visual Studio/MSBuild copy referenced class library's app.config as *.dll.config to bin folder of current project

我有一個由許多其他Web應用程序項目引用的類庫 它在app.config中有許多設置,我希望在所有引用Web項目中使用它們。 構建類庫時,它會將app.config復制到其自己的bin文件夾中,作為<assembly.name>.dll.config

如何確保將<assembly.name>.dll.config復制到每個引用Web應用程序項目的bin文件夾中?

  • 默認情況下,Visual Studio / MSBuild似乎不會執行此操作。
  • Copy to Output Directory更改Copy to Output DirectoryBuild Action (以任意組合)不起作用。
  • SlowCheetah VS擴展似乎做了我想要的作為其配置轉換過程的意外副作用,但我想這樣做沒有任何第三方擴展。

暫且不說:在每個Web應用程序項目中手動放置所有這些配置是不切實際的。 我可以通過在bin文件夾中查找<assembly.name>.dll.config來讀取該文件,並從那里提取設置。 我已經可以做到這一點,所以這不是問題 - 我只需要確保文件將在那里閱讀

通過將以下內容添加到類庫的項目文件assembly.name.csproj ,可以在沒有第三方工具的情況下實現此目的:

// Find this <Import> element for Microsoft.CSharp.targets
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
// Add this <ItemGroup> element immediately after
<ItemGroup>
    <Content Include="app.config">
        <Link>$(TargetName).dll.config</Link>
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
</ItemGroup>

這會導致app.config被復制到以<assembly.name>.dll.config引用它的任何項目。 這很好,因為您只需要配置一個.csproj文件,並將效果級聯到所有引用項目。

Visual Studio 2015及更早版本

對於Visual Studio 2015,我使用@theyetiman的解決方案(請參閱此答案 ):

<ItemGroup>
  <None Include="app.config" />
</ItemGroup>
<ItemGroup>
  <None Include="app.config">
    <Link>$(TargetFileName).config</Link>
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </None>
</ItemGroup>

Microsoft.CSharp.targets的導入已經存在。)

Visual Studio 2017

在我的實驗中,Visual Studio 2017似乎開箱即用地處理配置文件。 不需要手動修改項目文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM