XNA中使用directoryInfo时获取异常

XNA中使用directoryInfo时获取异常

本文介绍了在C#/ XNA中使用directoryInfo时获取异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


Hello Everybody!



想想我的Windows Phone应用程序有问题。在我的应用程序中,我有一个静态方法,它返回一个位于特定文件夹中的纹理集合。当我在WP 8 Emulator上启动我的应用程序时,它工作得非常好,但是当我在WP 7模拟器上启动
时,我得到一个异常,并显示消息"尝试访问该方法失败:System.IO .DirectoryInfo..ctor(System.String)"。



但事实上这个目录是存在的。有谁可以告诉我为什么会发生这种情况?



这里我附上了我的代码:



 namespace Hornbook_v2 
{
public static class ContentLoader
{
public static Dictionary< String, T> LoadTextures< T>(此ContentManager contentManager,字符串contentFolder)
{
//加载目录信息,如果没有则中止
DirectoryInfo目录=新DirectoryInfo(contentManager.RootDirectory +" \\ " + contentFolder);

//这个例外发生在这里!
if(!dir.Exists)
抛出新的DirectoryNotFoundException();

//初始化结果列表
Dictionary< String,T> result = new Dictionary< String,T>();

//加载与文件过滤器匹配的所有文件
FileInfo [] files = dir.GetFiles(" *。*");
foreach(文件中的FileInfo文件)
{
string key = Path.GetFileNameWithoutExtension(file.Name);

//结果[key] = contentManager.Load< T>(contentManager.RootDirectory +" /" + contentFolder +" /" + key);
result [key] = contentManager.Load< T>(contentFolder +" /" + key);
}

//返回结果
返回结果;
}

}
}





解决方案

Hello Everybody!

Think I have a problem with my windows phone application. In my application I have a static method that returns me a collection of textures located in specific folder. When I launch my application on the WP 8 Emulator it`s worked very well, but when I launch it on the WP 7 emulator I get an exception with the message "Attempt to access the method failed: System.IO.DirectoryInfo..ctor(System.String)".

But in fact that this directory is existing. Could anybody tell me why this happends ?

Here I attached my code:

namespace Hornbook_v2
{
    public static class ContentLoader
    {
        public static Dictionary<String, T> LoadTextures<T>(this ContentManager contentManager, string contentFolder)
        {
            //Load directory info, abort if none
            DirectoryInfo dir = new DirectoryInfo(contentManager.RootDirectory + "\\" + contentFolder);

            //  The exception is happends HERE!!!
            if (!dir.Exists)
                throw new DirectoryNotFoundException();

            //Init the resulting list
            Dictionary<String, T> result = new Dictionary<String, T>();

            //Load all files that matches the file filter
            FileInfo[] files = dir.GetFiles("*.*");
            foreach (FileInfo file in files)
            {
                string key = Path.GetFileNameWithoutExtension(file.Name);

                //result[key] = contentManager.Load<T>(contentManager.RootDirectory + "/" + contentFolder + "/" + key);
                result[key] = contentManager.Load<T>(contentFolder + "/" + key);
            }

            //Return the result
            return result;
        }

    }
}


解决方案


这篇关于在C#/ XNA中使用directoryInfo时获取异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 17:17