XNA: Load all content files in a folder
After playing around with XNA for a little while, I quickly realised how tedious it is to load all content manually, especially if the project uses a lot of images, sounds, textures etc.
The method below is an extension method that can parse all files within a folder into any content type. The method requires that the specified folder is relative to the Content.RootDirectory folder.
/// <summary>
/// Load all content within a certain folder. The function
/// returns a dictionary where the file name, without type
/// extension, is the key and the texture object is the value.
///
/// The contentFolder parameter has to be relative to the
/// game.Content.RootDirectory folder.
/// </summary>
/// <typeparam name="T">The content type.</typeparam>
/// <param name="contentManager">The content manager for which content is to be loaded.</param>
/// <param name="contentFolder">The game project root folder relative folder path.</param>
/// <returns>A list of loaded content objects.</returns>
public static Dictionary<String, T> LoadContent<T>(this ContentManager contentManager, string contentFolder)
{
//Load directory info, abort if none
DirectoryInfo dir = new DirectoryInfo(contentManager.RootDirectory + "\\" + contentFolder);
if (!dir.Exists)
throw new DirectoryNotFoundException();
//Init the resulting list
Dictionary<String, T> result = new Dictionary<String, T>();
//Load all files that matches the file filter
FileInfo[] files = dir.GetFiles("*.*");
foreach (FileInfo file in files)
{
string key = Path.GetFileNameWithoutExtension(file.Name);
result[key] = contentManager.Load<T>(contentManager.RootDirectory + "/" + contentFolder + "/" + key);
}
//Return the result return result; }
The function extends the ContentManager class, and can, for instance, be used by the main Game class like this:
-
var textures = Content.LoadContent<Texture2D>("Textures"); -
var models = Content.LoadContent<Model>("Models"); -
var songs = Content.LoadContent<Song>("Songs"); -
...
The method returns a dictionary, so if you want to access the “warrior” model in the models dictionary, you just have to access it as such:
-
var warriorModel = models["warrior"];
I am having some problems with pasting in code fragments into this blog, so if anyone can give me some tips for good plugins that can be used at wordpress.com, feel free to do so


EriX 1:17 am on August 10, 2010 Permalink |
This is great! thanks soooo much for that info
Gradius V 10:32 pm on February 8, 2011 Permalink |
Thanks for this tip =)
Cpt Fresh 7:06 pm on March 23, 2011 Permalink |
Thanks, this was actually exactly what I needed.
danielsaidi 9:33 am on March 24, 2011 Permalink |
Glad to hear it!
TXG1152 5:20 am on April 27, 2011 Permalink |
Thanks for the great solution. One note though, for XNA 4 it seems that the asset name in ContentManager.Load(asset) is relative to the content loader root directory. You will need to take contentManager.RootDirectory + “/” + out of the call or you will get an error. Try this instead:
result[key] = contentManager.Load(contentFolder + “/” + key);
Andy 8:59 am on October 26, 2011 Permalink |
Thanks, nice tip, but i have a question:
How can i convert this result (Dictionary) into Dictionary for example?
Code:
Dictionary buttonList;
buttonList = result; <— Error because of the conversion
thank in advance =)
Andy 9:02 am on October 26, 2011 Permalink |
Hmm there was a problem displaying my question properly:
“Dictionary(String, Texture2D) buttonList;”
“Dictionary(String, T) result;”
“buttonList = result;”
danielsaidi 9:37 am on October 26, 2011 Permalink |
Wow, I have not looked at this for a long while
However, let’s begin with – what is T in your example? Can T be cast to Texture2D?
Andy 10:32 am on October 26, 2011 Permalink |
T can be a Texture2D, SpriteFont or Sound or whatever
here another example:
buttonList is a Dictionary with String, Texture2D
fontList is a Dictionary with String, SpriteFont
and result is a Dictionary with String, T and T can be: SpriteFont, Texture2D, Sound
case “Other”: buttonList = result;
break;
case “Font”: fontList = result;
break;
danielsaidi 10:37 am on October 26, 2011 Permalink |
I may be wrong, but in order to make your code work, you need to constraint T so that it always can be cast to an Texture2D…at least for the example you provided. You can do this by adding “where T : Texture2D”. If T can be anything in your code, then buttonList will not be compatible with result.
Andy 11:29 am on October 26, 2011 Permalink |
“If T can be anything in your code, then buttonList will not be compatible with result.”
yeah and i wanted to implement a qucik and nice solution for this problem,
so that i dont have to look for the type of T , if its a Texture or Sound or a Font….
do u have any idea how to convert this without using if or switch?
Victor 2:50 pm on November 3, 2011 Permalink |
Humm, i had to make the following changes before it works for me:
result[key] = contentManager.Load(contentFolder + “\\” + key);
Victor 2:50 pm on November 3, 2011 Permalink |
my folder structure: Content\Set1\myFile.png
Steven 5:45 pm on March 6, 2012 Permalink |
This is exactly what I need, but I have a problem. I added the code as you wrote it and get no compilation errors. When I run the program I get a ContentLoadException, file not found, with result[key] = content.load < t>(contentManager.rootDirector + “/” + contentFolder + “/” + key);. I’ve tried making changes listed here but the same error comes up. I can’t figure it out. And the thing that baffles me the most is the fact that when I look at the file in the foreach loop, it shows that the file exists at that location.
Steven 10:54 pm on March 6, 2012 Permalink |
Thanks for the help that would have been given. I finally managed to figure out my issue. I has some weird things going on with my content directory, but a new project and it works fine. Thanks for the code.
danielsaidi 10:15 pm on March 9, 2012 Permalink
Glad that it worked out! This was such a long time ago that it is great to hear that it is still a working way to handle content
XNA ContentLoadException when loading textures in the main project using a load command in a DLL 1:18 pm on June 8, 2012 Permalink |
[...] found this code here, but even rewriting it into list form I get the same error. The weird part is that the Error [...]