I have a solution with two projects:
- Project A
- This is a C# console application.
- Project B
- This is a C# class library.
Project A references Project B via a project dependency.
Project B contains a folder, and in this folder are some files.
The properties of these files are set to:
Build Action
Content
Copy to Output Directory
Copy if newer
(Also tried withCopy Always
)
When I Build Project B, the files in the content folder are copied to the output folder for Project B.
e.g. ProjectB/bin/debug/contentfolder/files
When I Rebuild Project A, the files in the content folder are copied to the output folder for Project A.
e.g. ProjectA/bin/debug/contentfolder/files
When I Build Project A, the files in the content folder are not copied to the output folder for Project A.
e.g. ProjectA/bin/debug/contentfolder/NO FILES HERE
I could understand if Visual Studio "can't see" the files in dependency projects for some reason, and the files were never copied to ProjectA's output.
But that's not the case since it works during a rebuild.
So it feels like either I'm doing something wrong, and not noticing, or that Visual Studio is doing something wrong, and needs fixing.
I found this question which is similar to mine: Visual Studio Build vs Rebuild: Why are my DLLs only sometimes being copied into the output directory?
However it doesn't have any answers, and it's not a duplicate as I'm more explicit about my solution layout and the circumstances under which this behavior occurs.
There is an older question, with an accepted answer here: Why the native DLL is not copied to the output directory
But it says:
(NOTE: Their usage of Project A/B is opposite to mine.)
If a C# project B has direct reference to project A, VS will copy all dependent files used by project A to the output directory of project B.
Which is clearly not happening in my case, which is the basis of my question!
There are two work-arounds that seem obvious to me:
- The Content folder and files can be added to the console project.
- A post-build event can be used to copy the files after the console project has finished building.
But they both have the same downsides:
This shouldn't be necessary! The fact this works under Rebuild suggests that this should work for Build too, but something is going wrong.
This is a manual change which the developer has to be aware of and action when adding a reference to Project B. Simply adding the project reference and having it all work is much less error prone.