Sign in to follow this  
gchewood

Problem loading content folder automatically

Recommended Posts

gchewood    236
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:
[url="http://danielsaidi.wordpress.com/2010/01/26/xna-load-all-content-files-in-a-folder/"]http://danielsaidi.w...es-in-a-folder/[/url]
though I've adjusted it slightly for my own purposes.

Here's the method I'm using:

[CODE]
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));
}
}
[/CODE]

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

Share this post


Link to post
Share on other sites
gchewood    236
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.

Share this post


Link to post
Share on other sites
MisterFuzzy    161
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.

Share this post


Link to post
Share on other sites
gchewood    236
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.

Share this post


Link to post
Share on other sites
phil_t    8084
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".

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this