在 .csproj 中包含项目范围之外的内容文件
我有一个 C# 项目 MyProject.csproj 位于“C:\Projects\MyProject\”。 我还有想要复制到该项目的输出目录中的文件。 但是,这些文件位于“C:\MyContentFiles\”位置,即它们不在项目范围内。 该目录也有子目录。 目录的内容不受管理。 因此我必须包括它下面的所有内容。
当我将它们作为“内容”包含在项目中时,它们会被复制,但目录结构会丢失。 我做了这样的事情:-
<Content Include="..\..\MyContentFiles\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
如何将这些文件/目录递归复制到项目的输出目录中并保留目录结构?
I have a C# project say MyProject.csproj located at "C:\Projects\MyProject\". I also have files that I want copied into the output directory of this project. But, the files are at the location "C:\MyContentFiles\", i.e. they are NOT within the project cone. This directory has sub-directories as well. The contents of the directory is not managed. Hence I have to include all what is under it.
When I include them as 'Content' in the project, they are copied, but the directory structure is lost. I did something like this:-
<Content Include="..\..\MyContentFiles\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
How do I copy these files/directories recursively into the output directory of the project with the directory structure preserved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您将添加到项目文件底部的以下内容将在构建后事件中将维护目录结构的内容文件复制到构建的目标目录
$(TargetDirectory)
(通常为 <代码>$(MSBuildProjectDirectory)\bin\Debug )。如果这些文件需要放入名为 MyContentFiles 的目录中,您可以在复制之前添加以下内容:
并更改
为
The following, which you would add to the bottom of your project file, will copy your content files maintaining the directory structure in a after build event to the target directory
$(TargetDirectory)
of your build (typically$(MSBuildProjectDirectory)\bin\Debug
).If these files needed to go in a directory named MyContentFiles, you could add this before the copy:
and change
To
Mandark 的答案将内容文件直接添加到解决方案中,它们将显示在解决方案资源管理器中。 每当在原始目录中添加或删除文件时,Visual Studio 不会自动拾取该文件。 此外,每当在解决方案资源管理器中删除或添加文件时,项目文件都会更改,并且所有文件都会单独包含,而不仅仅是包含文件夹。
为了防止这种情况,您可以使用相同的技巧,但将其放入单独的项目文件中,然后导入。
项目文件(例如 include.proj)如下所示:
在您自己的项目文件中,添加以下行
Visual Studio 不会弄乱此文件,而只会在生成期间添加文件作为内容。 始终包含原始目录中的更改。 这些文件不会显示在解决方案资源管理器中,但将包含在输出目录中。
在这里学到了这个技巧: http://blogs.msdn.com/b/shawnhar/archive/2007/06/06/wildcard-content-using-msbuild.aspx
The answer of Mandark adds the content files directly to the solution, and they will show up in the solution explorer. Whenever a file is added or deleted in the original directory, this is not picked up by visual studio automatically. Also, whenever a file is deleted or added in the solution explorer, the project file is altered, and all files are included separately, instead of just including the folder.
To prevent this, you can use the same trick, but put it in a separate project file and then import it.
The project file (for example include.proj) looks like this:
In your own project file, add the following line
Visual Studio will not mess with this file, and just adds files as content during a build. Changes in the original directory are always included. The files won't show up in your solution explorer, but will be included in the output directory.
Picked up on this trick here: http://blogs.msdn.com/b/shawnhar/archive/2007/06/06/wildcard-content-using-msbuild.aspx
您需要将文件添加为链接:
但是您不能对目录树执行此操作。
相反,您需要为此编写构建后任务。 这是一个让您感兴趣的示例。
You need to add file as a link:
BUT You cannot do it for the directory tree.
Instead you need to write post-build task for that. This is a sample that will get you stared.
我相信 @Dmytrii 一方面是正确的 - 你想使用“链接”功能。
然而,当他说不能链接到目录树时,他只说对了一部分。
虽然在尝试使用 Visual Studio 的 GUI 添加链接时确实如此,但 MSBuild 支持这一点。
如果要保留目录结构,只需将
%(RecursiveDir)
标记添加到节点即可:
页面 MSBuild 知名项目元数据 详细介绍了您可以访问的元数据。
I believe @Dmytrii gets it right on one hand - you want to use the "link" feature.
However, he's only partly correct when saying you can't link to a directory tree.
While this is, indeed, true when trying to add the links using Visual Studio's GUI, MSBuild supports this.
If you want to preserve the directory structure, just add the
%(RecursiveDir)
tag to your<link>
node:The page MSBuild Well-known Item Metadata goes into more detail on the metadata you can access.
要将文件包含在 .NET Core 项目的文件夹中,
并且项目的属性“复制到输出目录”将为“如果较新则复制”:
To include files in a folder for a .NET Core project,
And the items' property "Copy to Output Directory" will be "Copy if newer":
我认为
就足够了,因为您想要该文件夹和子文件夹中的所有内容
I think
Is just enough, since you want everything in that folder and subfolders
要忽略 .Net Core 项目中的文件:
To ignore a file in a .Net Core project: