Visual Studio/MSBuild copy referenced class library#39;s app.config as *.dll.config to bin folder of current project(Visual Studio/MSBuild 将引用类库的 app.config 作为 *.dll.config 复制到当前项目的 bin 文件夹)
问题描述
我有一个被许多其他 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 文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Visual Studio/MSBuild 将引用类库的 app.config 作为 *.
基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01