Loading Multiple Content Files

The content pipeline is fantastic, but it can get tedious having to define individual files and loading them in code. A better way is to load an entire directory.
Create a class called ContentUtils...it doesn't have to be but that is the name I'll be using. It'll be using entirely static functions so feel free to declare the class as such.
The first method retrieves a list of all the content files (.xnb) in a specified path.

 
				 
private  static FileInfo[] RetrieveXnbFileInfo(string directoryPath)
{
	DirectoryInfo dir = new DirectoryInfo(directoryPath);

	if (!dir.Exists)            
		Directory.CreateDirectory(directoryPath);

	FileInfo[] files = dir.GetFiles("*.xnb");
	return files;
}
				
			

All thats happening here is checking that the directory exists, creating it if it doesn't and then getting a list of the files. Of course, you may want to change this behaviour e.g. silently continue and not create, throw an exception etc.
My uses found this sufficient.

Extracting filename strings

 
				 

public  static List<string> RetrieveFileNames(ContentManager content, string subDir)
{
	List<string> result = new List<string>();

	FileInfo[] files = RetrieveXnbFileInfo(content.RootDirectory + "/" + subDir);

	foreach(var file in files)
	{
		string name = file.Name.Split('.')[0];
		result.Add(n);
	}

	return result;
}

				
			

So here, the code is simply getting the filenames for a specified sub-directory of your Content's root directory. Typically the root directory is defined in the Game class' constructor. If you haven't done this (though it is generated with the Monogame template), make sure to use the following code:

 
				 
Content.RootDirectory = "MyContentDirectoryName";  
				
			

It is a good idea to add code to check for use of "/" at the end of a path when defining these functions. I'm leaving such code out for clarity.

Putting it together

Finally, the main function that will load all our glorious pictures and sound files. Substitute T for you asset type. (Texture for example):

 
				 
 public  static Dictionary<string,T> LoadContentDir< T >(ContentManager Content,string subDir)
{          
	string contentDir = Content.RootDirectory + "/";

	FileInfo[] files = RetrieveXnbFileInfo(contentDir + subDir);

	Dictionary<string, T> result = new Dictionary<string, T>();

	 foreach (var file in files)
	{
		string name = file.Name.Split('.')[0];

		result.Add(name,Content.Load(subDir + na,e));

	}

	Console.WriteLine(result.Count + " content files loaded from " + subDir);
	return result;
}
				
			

So as you can see, the function will return to you a Dictionary with your Content assets. They'll be keyed by their name as defined by the Pipeline Tool (i.e. filename minus the extension).
You can use any key you like, but this guide just gives you a means to load assets a more convieniently. I use this code as part of a system for dynamically loading sounds and music. Full source.

Email: magellanicDev@gmail.com
Last Updated: 16/02/2019
Copyright: Magellanic Games LTD

Generated with SpeedyHtml.