Windows Phone 7 crash when accessing directory

放肆的年华 提交于 2019-12-25 00:14:11

问题


I am having trouble getting info on a directory in Windows Phone 7 with C#. My application freezes or crashes when I call:

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("Content/Levels");

It also freezes when I try to determine the path of the application so I think this is where the problem lies.

What am I possibly doing wrong and what can I do to fix this?

Update: It seems this problem is related to a sequirity issue but I don't know how to resolve this. Is there possibly another way to find the root directory withouth this sequirity isue?

If it makes a differences these files are stored in the XNA content pipeline.


回答1:


To access a file within the actual xap (known as "title storage"), first of all you must export it as Content from the XNA Content Pipeline. Select the file(s) from your Content Project and under Properties set its Build Action to Content (I believe None does the same thing). Now they will export directly, raw, to your XAP and are accessible within your game/app.

Secondly, to access files from the title storage, you can't simply access the root directory directly on WP7. Use this class to access files from the title storage:

using (StreamReader r = 
new StreamReader(TitleContainer.OpenStream("Content/Levels/Level1.xml")))
{
    //read the file
}

The method returns a System.IO.Stream that you can use for direct access into the file itself.

If your game needs to know all the files within a particular folder, directory enumerating and the like is not really allowed on this platform, so you would have to generate it beforehand at compile time, which is possible using the Content Pipeline:

http://xbox.create.msdn.com/en-US/sample/contentmanifestextensions



来源:https://stackoverflow.com/questions/15163455/windows-phone-7-crash-when-accessing-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!