Automatic copy files to output during application building

Loom picture Loom · Apr 25, 2011 · Viewed 34.4k times · Source

There is Copy to Output Directory property for files in C# projects. But in VC++ projects it is absent. I know, that I can use Build events in VC++ and write there something like

xcopy /y /d %(FullPath) $(OutDir)

Is there a way to avoid the use of CMD (and other scripting methods)? Can msbuild do something to help in this case?

Answer

jochen picture jochen · Nov 5, 2012

Can MSBuild do something to help in this case?

Using MSVC 2012, this worked for me:

Assumed you have a file "Data/ThisIsData.txt" in your c++ Project.

Unload the project (right click --> Unload Project).
Edit project XML (right click --> Edit .vcxproj)
Now you see the projects MSBuild file as XML in your editor.

Find "ThisIsData.txt". It should look something like:

<ItemGroup>
<None Include="Data\ThisIsData.txt" />
...
</ItemGroup>

Now add an other item group like this:

<ItemGroup>
<Content Include="Data\ThisIsData.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
...
</ItemGroup>

Reload the project and build.
Your file "ThisIsData.txt" should get copied to $(OutDir)\Data\ThisIsData.txt.

Why duplicating the ItemGroup?

Well if you simply change the None include to a content include, the IDE does not seem to like it any more, and will not display it. So to keep a quick edit option for my data files, I decided to keep the duplicated entries.