views:

173

answers:

2

I have a problem with the "Copy to output" functionality in Visual Studio 2008. Maybe I'm misunderstanding how its supposed to work.

I have a solution with four projects:

  • TestApp1 (Windows application)
  • TestAppA (Windows application)
  • TestProj1 (Class library)
  • TestProjA (Class library)

The dependencies are as follows (dependency as in project reference).

  • TestProj1 depends on nothing.
  • TestApp1 depends on TestProj1
  • TestProjA depends on TestProj1
  • TestAppA depends on TestProjA (and hence indirectly on TestProj1)

In other words, the dependency tree looks like this.

TestApp1
    TestProj1
TestProjA
    TestProj1
TestAppA
    TestProjA
        TestProj1

(each project has a reference to the standard assemblies (system, system.core, etc) as well)

In TestProj1, I have added a text file, Test.txt. In the properties for this file, I have specified "Build Action: Content" and "Copy to Output Directory: Copy always".

When I build the solution, Test.txt is copied to

  • TestApp1\bin\Debug
  • TestProj1\bin\Debug
  • TestProjA\bin\Debug

The file is not copied to TestAppA\bin\debug and this is what I find weird. Since TestProj1 relies on Test.txt to work, and TestAppA relies on TestProj1, via TestProjA, TestApp1 won't work.

If I add a project reference from TestAppA directly to TestProj1 (so I have one reference to TestProj1 and another to TestProjA), the file will be copied to the TestAppA\bin\debug folder.

Of course, I could set up a custom build event to copy the file Test.txt to TestAppA\bin\debug, but I have bad experiences with custom build events and would prefer to rely on the built-in copy mechanisms.

So my question is: Why isn't Test.txt copied to TestAppA\Bin\debug?

+1  A: 

Visual Studio only looks at direct references for content files, so TestAppA doesn't know that Test.txt exists since it doesn't see it in the TestProjA project file.

This is a limitation of Visual Studio, and is why many people end up pointing the output of all of their projects to the same folder.

The other option you have, is to create a "SharedContent" folder. You can then put the text file in that folder, and add it to each project that depends on it by selecting "Add > Existing Item..." on each project. In the Add Item dialog, select the text file from the shared folder, and click the little down arrow on the "Add" button. If you select "Add as Link", you can add the file to your project without creating a copy.

Todd Benning
+3  A: 

Todd's answered well already, but two more common solutions to this are:

  • add a post-build step to your dependent projects that xcopy the Test.txt into their output folder
  • add the text file as an embedded resource in your TestProj1 project's assembly and get rid of the need for an external data file entirely

I'd also suggest that all your dependencies could simply call an API in TestProj1 to retrieve the information they require, allowing that assembly to encapsulate the data and store it wherever, however, and in whatever format it likes.

Jason Williams
+1 for the encapsulation suggestion.
Todd Benning