Search code examples
c#visual-studiobuildvisual-studio-2019

How to copy project's resources folder to output directory in project with a reference to it


I have multiple projects in my solution. One of the projects has resources files (images). Then I have another project with unit tests. I'd like to have all these files available in unit tests project when the project with resources is referenced.

I've tried two solutions:

a) Apply a post-build script to the project with resources

The script copies resources directory content to the output (Debug/Release) directory as I need. But the script isn't executed when unit tests project is build (only when the project with resources is)

b) Set "Copy to Output Directory" property to value "Copy always" to all resources files

This works as I need even in project that has reference but I have to set this property manually to all the files. I wonder whether there is a way how to manage the same result but with a folder (to automatically copy all its content)

My question is: Is there any way how to automatically copy project's resources directory and its content to another project when the project with resources files is referenced?


Solution

  • You can check this link.

    https://github.com/dotnet/project-system/issues/3203

    As the link describes, if files are added to the directory during the build process, you must use a target to expand the wildcard, or it will only copy the files that existed at the start of the build.

    You can use the following methods:

    Open the *.vcxproj file and add the following code.

    <ItemGroup>
     <None Include="$(SolutionDir)config\**" CopyToOutputDirectory="PreserveNewest" />
    </ItemGroup>
    

    Hope it can help you.