Loading content from subfolders

Originally posted to Shawn Hargreaves Blog on MSDN, Saturday, June 2, 2007

A common pattern in XNA games is to nest a "Content" folder inside the main game project, to keep things organized. We do this in all the samples available on the creators.xna.com site, for instance.

When you come to load a piece of content, you have to write:

    Model ungulate = content.Load<Model>("Content/Dromedary");

or as some people prefer:

    Model ungulate = content.Load<Model>(@"Content\Dromedary");

Did you know there is an easier way? You can specify a root folder in the constructor where you initialize your ContentManager. Change this line from the standard game template:

    content = new ContentManager(Services);

to:

    content = new ContentManager(Services, "Content");

Now you can load content without bothering to specify the root folder:

    Model ungulate = content.Load<Model>("Dromedary");

So why don't we do this in our samples, or even build it in to the default game template?

Great question! I guess we just didn't think of it in time :-)

Blog index   -   Back to my homepage