[英]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文件夾中? Copy to Output Directory
更改Copy to Output Directory
或Build Action
(以任意組合)不起作用。 暫且不說:在每個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,我使用@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似乎開箱即用地處理配置文件。 不需要手動修改項目文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.