Search code examples
xnaloadingspritefont

XNA ContentLoadException from array given by Directory.GetFiles when file is clearly there


First I would like to state that this is not a Content.RootDirectory = "Content"; problem. I've tried this with and without that line of code. I have a class called FileFactory that has three dictionaries. One for textures, one for sounds, and one for sprite fonts. I also have other factories that load in XML files for other types of objects and those work perfectly. Here's the code that loads the fonts:

        public static void LoadFonts(ContentManager content)
    {
        string[] filePaths = Directory.GetFiles(@"Content\fonts\");
        string key;

        foreach (string s in filePaths)
        {
            key = Path.GetFileNameWithoutExtension(s);
            Fonts.Add(key, content.Load<SpriteFont>(s));
        }
    }

The error it gives is: "Error loading "Content\fonts\ammoFont.xnb". File not found." even though the file is CLEARLY there considering it would have to be in the directory to be added to the filePaths array as a string. I've even checked the folder and it is there. Here is the full error:

Microsoft.Xna.Framework.Content.ContentLoadException was unhandled
Message=Error loading "Content\fonts\ammoFont.xnb". File not found.
Source=Microsoft.Xna.Framework StackTrace: at Microsoft.Xna.Framework.Content.ContentManager.OpenStream(String assetName) at Microsoft.Xna.Framework.Content.ContentManager.ReadAsset[T](String assetName, Action`1 recordDisposableObject) at Microsoft.Xna.Framework.Content.ContentManager.Load[T](String assetName) at OfUnknownOrigin.FileFactory.LoadFonts(ContentManager content) in C:\Users\Chris\Documents\Visual Studio 2010\Projects\WindowsGame1\WindowsGame1\WindowsGame1\FileFactory.cs:line 38 at WindowsGame1.UnknownOrigin.LoadContent() in C:\Users\Chris\Documents\Visual Studio 2010\Projects\WindowsGame1\WindowsGame1\WindowsGame1\Game.cs:line 110 at Microsoft.Xna.Framework.Game.Initialize() at WindowsGame1.UnknownOrigin.Initialize() in C:\Users\Chris\Documents\Visual Studio 2010\Projects\WindowsGame1\WindowsGame1\WindowsGame1\Game.cs:line 102 at Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun) at Microsoft.Xna.Framework.Game.Run() at WindowsGame1.Program.Main(String[] args) in C:\Users\Chris\Documents\Visual Studio 2010\Projects\WindowsGame1\WindowsGame1\WindowsGame1\Program.cs:line 15 InnerException: System.IO.FileNotFoundException Message=Error loading "Content\Content\fonts\ammoFont.xnb.xnb". File not found. Source=Microsoft.Xna.Framework StackTrace: at Microsoft.Xna.Framework.TitleContainer.OpenStream(String name) at Microsoft.Xna.Framework.Content.ContentManager.OpenStream(String assetName) InnerException:

Answer: Change this code:

Fonts.Add(key, content.Load<SpriteFont>(s));

to this:

Fonts.Add(key, content.Load<SpriteFont>("fonts\\" + key));

Solution

  • From the exception message, it looks like the string you are calling it with is wrong. Content is in there twice, and the extension is there twice:

    Error loading "Content\Content\fonts\ammoFont.xnb.xnb"
    

    You didnt post the code where you initialize the array, but if you have them in the definition, remove them.