# ResourceContentManager with folders

I am attempting to do something like the following: http://blogs.msdn.com/b/shawnhar/archive/2007/06/12/embedding-content-as-resources.aspx (Access the content via a DLL)

However, I need to maintain the folder structure of the fonts and images. I have attempted to add the folders to the resources file, but it doesn't add the folder if I drop it in the resources file in VS. I am thinking I might need to embed the content as just a regular folder, and each file will need its property to be set to embedded resource, but I need to use it in another assembly.

I looked at: http://zacharysnow.net/2010/07/03/xna-load-texture2d-from-embedded-resource/ but I think I can only do that with textures. I need to do it with fonts as well.

I looked at: https://stackoverflow.com/questions/859699/how-to-add-resources-in-separate-folders and that doesn't seem to help me much.

Whats the best way I can embed my resources in another assembly, while maintaining my content loading strings?

• Hi, I'm the author on the post about embedding textures. My article skips over the content pipeline and as far as I know, SpriteFont does not have a FromStream method. Maybe this article can be of more help to you: blogs.msdn.com/b/shawnhar/archive/2007/06/12/…. – smack0007 Feb 3 '13 at 13:31

Right click the .resx file and select "View Code". You'll find a tag like the following:

<data name="Blah" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Resources\Blah.xnb;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>


The name that ResourceContentManager searches for is the name attribute in the Data tag. The location that the file comes from (in the value tag) doesn't matter. While it does cause the compiler to emit a warning, the easiest way to get the resource to be loaded by the name you want you want is to rename it to "Folder\Blah" or similar (note the back slash). There is no concept of folders here - it's just a name.

I am having a lot of difficulty verifying this - but "Embedded Resource" (the build option) seems to be different to embedding using a resx file. ResourceContentManager does not support loading files embedded this way.

However, it is pretty simple to create a class like ResourceContentManager to load "Embedded Resource" resources. Here's some tested, working code to do exactly that:

class EmbeddedResourceContentManager : ContentManager
{
public EmbeddedResourceContentManager(IServiceProvider serviceProvider)
: base(serviceProvider) { }

protected override Stream OpenStream(string assetName)
{
assetName = "YourNamespace." + assetName.Replace('\\', '.') + ".xnb";
return Assembly.GetExecutingAssembly().GetManifestResourceStream(assetName);
}
}


Note the way it converts the supplied path into the path expected by GetManifestResourceStream. If you have trouble with getting the path to work - check what the actual paths are with GetManifestResourceNames.

Doing it this way will save you having to create and manage a .resx file. And it will allow you to use the folder structure as exposed in Visual Studio.