0

I have just taken it upon myself to try out programming a very rudimentary game engine, starting by just displaying simple tiles in a 2d world in XNA. This is my first time writing a DLL, and it was all going well until I tried to write a LoadAll function for ContentManager, which has led to the problem I am having now. I am getting a ContentLoadException, even though the files clearly exist as otherwise the program would not get to the content loading stage.

The XNB files definitely exist, and the Content.RootDirectory has been set. I have been through every question like this I can find both here and on other sites and cannot find any solution. If anyone could help out I'd be eternally grateful.

    public static Dictionary<String, T> LoadAll<T>(this ContentManager Content, string directory)
    {
        DirectoryInfo dir = new DirectoryInfo(Content.RootDirectory + "/" + directory);
        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] = Content.Load<T>(dir.ToString() + "/" + key);
        }
        return result;
    }

Obviously I'm getting the error at the Content.Load method, where I get

"Error loading "Content\Tiles\bricktile". File not found."

I found this code here, but even rewriting it into list form I get the same error. The weird part is that the Error Loading "path to file" shows the correct path to an xnb file that definitely exists. If anyone wants the source for my DLL, I can easily upload that.

Thanks!

Edit: Should really have said how I call this:

textures = Content.LoadAll<Texture2D>(tilesFolder);

1 Answer 1

1

You pass the wrong path to the Content.Load<>() method. dir.ToString() will return the complete path (including the content's root directory). But you need the path without the root directory:

result[key] = Content.Load<T>(directory + "/" + key); 
1
  • Thanks, had been poring over this for ages. Rookie mistake!
    – Jobat
    Jun 14, 2012 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.