问题
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