How to automatically copy embedded resources from class library that is referenced by another class library?

Aaron picture Aaron · Mar 26, 2010 · Viewed 9.8k times · Source

Here is my project structure:

  • Web Application Project
    • Has project reference to Class Library 1 in same Visual Studio 2008 solution
  • Class Library 1
    • Has project reference to Class Library 2 in same solution
  • Class Library 2
    • Has a file marked as Build Action: Embedded Resource and Copy to Output Directory: Copy Always

Here is my problem:

When I build my Web Application Project, the embedded resource in Class Library 2 is NOT copied to the bin directory of the Web Application Project as it should. If I add a project reference directly from my Web Application Project to Class Library 2, the file IS copied to the bin directory.

How can I get the embedded resource file to copy without having to add the unnecessary reference?

Answer

devuxer picture devuxer · Mar 26, 2010

I think you need to mark it Content + Copy Always.

Refer to this page for details on the different Visual Studio file properties.


Update 1

Try creating a link to the file in your web application project. You can do this by...

  1. Right-click your web project (or a folder inside it) using Solution Explorer
  2. Select Add > Existing Item
  3. Navigate to your file and select it
  4. Click the little arrow on the right side of the Add button and choose Add As Link

Update 2

Using Content / Copy Always on any file in a referenced library project should cause the file to be copied to the bin folder of the main project. If the file is in a folder, it will be in that folder in the bin folder.

Make sure that if you are doing a Release build, you are looking bin/Release, and if you are doing a Debug build, you are looking in bin/Debug.

Also, try doing a Rebuild All.


Update 3

I see what you're saying now. When you reference a library DLL that is not in your solution, you are only referencing the DLL, not anything else that is in that project.

So, I think you are going to need to do one of the following...

  1. Include that project in your solution
  2. Link to the file (as in Update 1)
  3. Create a new project called something like MyLibraryContentFiles that has only the content files of the library project, but not the code, and include this project in your solution. You might find that separating the content files from the code is advantageous anyway.
  4. Embed any files you need within the library project (DLL) (using Embedded Resource), then provide a property on a public class that provides some kind of interface for referencing the file. If it's an image file, you could return an Image object. If it's a text file, you could return a plain string.