Loading content from subfolders


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 🙂


Comments (6)

  1. Mykres Space says:

    Shawn has posted a nice quick one today on the location of Content in you XNA Games , and it is something

  2. zygote says:

    it is something indeed! 😉

  3. Honmiz says:

    Thanks for your articles. I have learned a lot.

  4. jumptack says:

    Is it possible to load a model outside of the content Manager?  Directly from the disk?  Thanks

  5. Shawn has posted a nice quick one today on the location of Content in you XNA Games , and it is something that I all think we might be able to use. I do know that with the small project that I am working on, this is going to be another small piece of

  6. Altobot says:

    I got a Problem, how to get Contents into the Solution Explorer?

    Thanks for replying

Skip to main content