問題描述
我有一個被許多其他 Web 應用程序項目引用的類庫.它的 app.config
中有許多我希望在所有引用 Web 項目中使用的設置.構建類庫時,它會將 app.config
作為 .dll.config
復制到自己的 bin
文件夾中.
I have a class library that is referenced by many other web application projects. It has many settings in its app.config
that I wish to use in all of the referencing web projects. When the class library is built, it copies the app.config
to its own bin
folder as <assembly.name>.dll.config
.
- 默認情況下,Visual Studio/MSBuild 似乎不這樣做.
- 更改
Copy to Output Directory
或Build Action
(任意組合)不起作用. - SlowCheetah VS 擴展似乎可以做我想做的事情,這是其配置轉換過程的意外副作用,但我想這樣做 沒有任何第 3 方擴展.
- Visual Studio / MSBuild does not seem to do this by default.
- Changing
Copy to Output Directory
orBuild Action
(in any combination) does not work. - SlowCheetah VS extension appears to do what I want as an unintended side-effect of its config transform process, but I want to do this without any 3rd-party extensions.
順便說一句:手動將所有這些配置放入每個 Web 應用程序項目中是不切實際的.我可以通過在 bin 文件夾中查找 <assembly.name>.dll.config
來讀取文件,然后從那里提取設置.我已經可以做到這一點,所以這不是問題 - 我只需要確保文件可以在那里閱讀
As an aside: It's impractical to manually put all of this config in each of the web application projects. I can read the file by looking for <assembly.name>.dll.config
in the bin folder, and extract the settings from there. I can already do this, so that's not an issue - I just need to ensure the file is going to be there for reading
推薦答案
這可以通過將以下內容添加到類庫的項目文件 assembly.name.csproj
來實現,無需 3rd-party 工具:
This can be achieved without 3rd-party tools by adding the following to the class library's project file, 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
被復制到將其作為 .dll.config
引用的任何項目.很好,因為你只需要配置一個.csproj
文件,效果就級聯到所有引用的項目.
This causes the app.config
to be copied to whichever project is referencing it as <assembly.name>.dll.config
. It's good because you only need to configure the one .csproj
file and the effect cascades out to all referencing projects.
這篇關于Visual Studio/MSBuild 將引用類庫的 app.config 作為 *.dll.config 復制到當前項目的 bin 文件夾的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!