Problem loading content folder automatically

Started by
4 comments, last by phil_t 11 years, 4 months ago
I'm writing a level editor for XNA and would really like to be able to load all of the models in my content folder automatically (so I can add new content without having to write new code).

I'm basing my code on that found in this article:
http://danielsaidi.w...es-in-a-folder/
though I've adjusted it slightly for my own purposes.

Here's the method I'm using:


public void LoadContent(ContentManager contentManager, List<Model> li)
{

DirectoryInfo dir = new DirectoryInfo(contentManager.RootDirectory);
if (!dir.Exists)
throw new DirectoryNotFoundException()
FileInfo[] files = dir.GetFiles("*.fbx");
foreach (FileInfo file in files)
{
string key = Path.GetFileNameWithoutExtension(file.Name);
li.Add(contentManager.Load<Model>(key));
}
}


The code runs just fine but doesn't add any model files to the list. There are definitely 4 fbx files in my content folder. I even tried adding them in the project (which I'd assumed wouldn't be necessary). Hope someone can help me out, I really don't wanna have to constantly change the code!
Thanks
Advertisement
You would have an invalid path.
Please consider checking contentManager.RootDirectory.
Not entirely sure what you're saying. How would my path be invalid? I set the rootdirectory and use it to load content elsewhere in the project so I somehow doubt that's the problem.
Also, that would throw the DirectoryNotFoundException(). Which it doesn't.
The root usually isn't your Content folder, it's the application root... So you still need to navigate to your content folder. This may or may not be the case (I usually have to set the directory myself), but try printing a debug string with the value "content.RootDirectory" to see where it's going.

"Only idiots quote themselves" - MisterFuzzy

I set content.RootDirectory in the game's constructor (as I thought was done everyone) and it's used to load all of the others game content so that's definitely not the problem.
Yeah, it should be the content folder, so you're good there. You're sure the files are there in the output Content folder? (e.g. [yourgame]/bin/x86/Debug/Content/....)

You said you added them to the content project - but what did you do to ensure they end up getting copied to the output Content folder? By default, "Copy to Output Directory" is set to "Do not copy". Make sure you've changed it to "Copy if Newer".

This topic is closed to new replies.

Advertisement