Gabriel Butcher
Gabriel Butcher

Reputation: 56

Strange C# Directory.GetFiles error

Today I ran into a very strange c# error.

I wrote a small method, which should search and process all the XNA's xnb file in any given directory. In most computer, it worked just fine - expect some cases (two guy with XP reported problem).

I was only able to reproduce this error on win7 one time (and off course, outside the VC#)

Here is the method:

files = Directory.GetFiles("Content\\Textures\\Houses\\Roof");

for (int i = 0; i < files.Length; i++)
{
    string path = files[i].Substring(BasicUtils.FindsubString(files[i], "Textures")).Replace(".xnb", "");

    roofTextures.Add(DataCenter.AddTexture(DataCenter.Content.Load<Texture2D>(path)));
}

This method get all the files in the given directory, in this case, "Content\Textures\Houses\Roof". And here come the error: there is three file in this directory (all with .xnb extension). On my PC with win7 (and four other guy's PCs with win7 as well) this work like as it should.

But, when someone with win XP try to run this method, the OS give back 4 file - and the three really exiting file, and one file opened last time with the OpenFileDialog (even if the file in completly different directory, the Directory.Getfiles give it back like it is there, in that directory).

And when I want to open it, the method dont find the file, and throw an exception.

I was able to overcome this error by checking if the given back file have an .XNB extension, but this error really make me wonder.

Any idea where I made an error?

The openfiledialog in a winform area, while the file searching and opening in another DLL, a completly different class, in a private, unaccesible method - they shouldnt even communicate in any way! I dont have any idea how the hell I got back that file name.

Edit:

I think I didnt made my problem clear (sorry for that :).

So, the problem in the method: when I use the Directory.GetFiles(...) its give back three files which are really in the directory (three .xnb file). But for some user (using windows XP) this method give back one .sbm extension (file type my program use) which ISNT inside the directory. I searched pretty long until I found out: this problem only pop up when they use the Openfiledialog - when they do, the Directory.GetFiles(...) will give back the previously opened file as it there - but phyisically it isnt. I was able to workaround this problem, but still cant understand WHY this can happen. I seached through the MSDN database, but I didnt found out anything about a bug like this. I tried to recreate the bug inside the VS hoping it will give a clue - but I wasnt able to do. One time the problem appeared for me as well (from win7) but as it was outside from the VS I wasnt able to debug. Currently only two win XP user reported, while its works fine for everyone using win7.

Oh, and I double checked - there is no hidden file (especially the previously opened file) in that directory. Only the three desired file inside.)

Edit 2:

Here is the link for the problem, and the steps to create the problem:

https://www.dropbox.com/s/4uv1hbvzkhpwprw/House%20Creator%20V2%20With%20error.zip

Start the Housecreator.exe Click the "Load" button. Select the "Suburb house 1.sbm"

When its loaded, click the "3D render" button.

And this is step where the error should pop up. The program will search for the "Suburb house 1.sbm" file in the (Content)\Textures\Houses\OuterWall (sometimes. mostly for xp users, but sometimes it happening for me as well. But only outside from the Visual studio).

XNA framework 3.1 will be needed, and .NET 3.5 as well to try out the program. And the error only pop up if the user use the "load" option.

Edit 3: I got the error three times in the row. No extra file was inside the given directory. And now its working again, without any visiable change O.o

Im running out from ideas why is this happening.

Edit 4:

I given up. Im pretty sure I making a mistake somewhere (I dont think that could be this huge bug, and nobody else noticed yet) but cant catch where is it. I filtering the files, and that should work. Thank you all for trying to help! :)

Upvotes: 0

Views: 1640

Answers (1)

Jf Beaulac
Jf Beaulac

Reputation: 5246

If Directory.GetFiles() returns 4 files, there are 4 files in the directory you specified as an argument.

You may not see them in Windows Explorer if they are hidden, protected system files, etc.

Anyway, your application should not expect to see only the files its concerned about in a given folder, you have no control over that.

The solution to your problem is to filter out files that you dont care about and ignore them.

for (int i = 0; i < files.Length; i++) 
{ 
    if(files[i].EndsWith(".xnb"))
    {
        string path = files[i].Substring(BasicUtils.FindsubString(files[i], "Textures")).Replace(".xnb", ""); 

        roofTextures.Add(DataCenter.AddTexture(DataCenter.Content.Load<Texture2D>(path))); 
    }
} 

Upvotes: 1

Related Questions