如何防止在增量构建过程中删除内容文件? [英] How to prevent content files to be removed during incremental build?
问题描述
我有一个包含3个项目的Visual Studio 2015 Update 3解决方案:
I have a Visual Studio 2015 Update 3 solution with 3 projects:
我的顶级应用程序是MyApp
,它在ClassLibrary2
上有一个项目引用,而在ClassLibrary1
上又有一个项目引用:
My top-level application is MyApp
which has a project reference on ClassLibrary2
which in turn has a project reference on ClassLibrary1
:
MyApp -> ClassLibrary2 -> ClassLibrary1
ClassLibrary1
具有一个数据文件data.bin
,这是其正常执行所必需的.预计将在与执行应用程序时ClassLibrary1
程序集所在的位置相对的Resources/
子文件夹中找到此数据文件. ClassLibrary1
的结构类似于:
The ClassLibrary1
has a data file, data.bin
, that is required for its normal execution. It is expected that this data file will be found in a Resources/
sub-folder relative to where the ClassLibrary1
assembly is when the application is executed. The structure of the ClassLibrary1
is similar to:
ClassLibrary1.csproj
Resources/
data.bin
MyClassBehaviorDrivenByData.cs
...
data.bin
文件的Build Action
属性设置为Content
(我也尝试过使用None
),其Copy to Output Directory
属性设置为Copy If Newer
.
The data.bin
file has its Build Action
property set to Content
(I have also tried with None
) and its Copy to Output Directory
set to Copy If Newer
.
如果我执行完整的解决方案重建,则data.bin
文件将按预期方式进入MyApp
的输出目录.但是,如果我在MyApp
项目中进行了一些更改并构建了解决方案,则会从MyApp
的输出目录中删除文件(注意:输出"窗口仅显示<因为仅更改了MyApp
的代码,所以构建了c0>.
If I do a full solution Rebuild, the data.bin
file finds its way to the output directory of MyApp
, as expected. However, if I do some changes in the MyApp
project and build the solution, the data.bin
file is deleted from the output directory of MyApp
(note: the Output window shows that only MyApp
is built since only the code of MyApp
was changed).
解决方法:
-
每次构建
MyApp
时,我都可以使用MyApp
中的构建后步骤将数据文件强制复制到输出目录中.但是,这违背了以下原则:开发人员拥有"ClassLibrary1
如果添加新数据文件,修改现有文件或将其移至其他位置,则不必担心在消耗项目中修改构建后脚本.
I could use post-build steps in
MyApp
to forcefully copy the data file to the output directory each timeMyApp
builds. However, this goes against the principle that the developer "owning"ClassLibrary1
should not have to worry about modifying post-build scripts in consuming projects if he adds a new data file, modify existing ones or move them elsewhere.
我可以直接从MyApp
添加对ClassLibrary1
的引用.但是,这违反了隐藏ClassLibrary2
API下的ClassLibrary1
功能/API的愿望(即,我不希望ClassLibrary1
API泄漏到MyApp
中).
I could directly add a reference to ClassLibrary1
from MyApp
. However, this violates the desire to hide away the ClassLibrary1
functionality/API under the ClassLibrary2
API (i.e., I don't want ClassLibrary1
API to leak into MyApp
).
我的问题是:是什么导致在MyApp
的增量构建期间从MyApp
的输出目录中删除data.bin
文件,并且有一种方法可以防止这种情况发生,而无需使用列出的两种解决方法之一以上?
My questions are: what causes the data.bin
file to be deleted from MyApp
's output directory during incremental builds of MyApp
and is there a way to prevent this to happen without using one of the 2 workarounds listed above?
推荐答案
我有同样的问题.
尽管我不太喜欢它,但我的解决方法是将文件data.bin包含在ClassLibrary2 probject中,并将复制到输出目录"设置为如果更新则复制".我还将ClassLibrary1中data.bin的相同属性设置为请勿复制".
My workaround, although I am not very fond of it, is including the file data.bin in the ClassLibrary2 probject and setting "Copy to Output Directory" to "Copy if newer". I also set the same property for data.bin in ClassLibrary1 to "Do not copy".
这篇关于如何防止在增量构建过程中删除内容文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!