0
\$\begingroup\$

I have an issue while trying to load a font file with the extension ".xnb" and I am receiving following error:

Error loading "SpriteFont1". File not found.

For following code:

public static Dictionary<string, T> LoadListContent<T>(this ContentManager contentManager, string contentFolder)
        {
            DirectoryInfo dir = new DirectoryInfo(contentManager.RootDirectory + "/" + contentFolder);
            if (!dir.Exists)
                throw new DirectoryNotFoundException();
            Dictionary<String, T> result = new Dictionary<String, T>();

            FileInfo[] files = dir.GetFiles("*.*");
            foreach (FileInfo file in files)
            {
                string key = Path.GetFileNameWithoutExtension(file.Name);


                result[key] = contentManager.Load<T>(key);
            }
            return result;
        }

The file is actually there and I am getting here FileInfo[] files = dir.GetFiles("*.*"); one file which is present in the folder.

I am using the function like this:

fontList = ContentLoader.LoadListContent<SpriteFont>(Content, "Fonts");

Is there something which I have overlooked?

\$\endgroup\$
2
  • \$\begingroup\$ using a breakpoint, what is the value of key? \$\endgroup\$
    – lozzajp
    Commented Nov 10, 2016 at 14:39
  • \$\begingroup\$ If it is "SpriteFont1.xnb" you may need to strip the extension? I forget how XNA content pipeline works. \$\endgroup\$
    – lozzajp
    Commented Nov 10, 2016 at 14:48

1 Answer 1

1
\$\begingroup\$

Reflecting the ContentManager.Load(assetName) method, it gives this note:

assetName:

Asset name, relative to the loader root directory, and not including the .xnb extension.


Even though files has the needed copy of "SpriteFont1" in it, the ContentManager isn't looking for the file in files. It's looking for "SpriteFont1" in the ContentManager's root directory /Content . Try changing the ContentManager's root directory to /Content/contentFolder.

Content.RootDirectory = "Content/contentFolder";//amend the line in the Game1 constructor

or change key to:

string key = "/contentFolder/" + Path.GetFileNameWithoutExtension(file.Name);
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .