如何读取XNA 4 for Windows Phone 7中的txt文件?

我看了一下之前的问题,但这似乎不适用于Windows Phone 7项目的XNA 4: XNA Framework Importers

我一直在尝试使用:

string line; using (StreamReader sr = new StreamReader("credits.txt")) { while ((line = sr.ReadLine()) != null) { //reads line by line until eof //do whatever you want with the text } } 

`

但这是抛出一个System.MethodAccessException“尝试访问该方法失败:System.IO.StreamReader..ctor(System.String)”

我需要考虑使用IsolatedStorage吗?

编辑:请注意我正在尝试加载预先准备好的文件,而不是保存正在运行的应用程序的设置。 例如,包含我将仅在运行时读取的信用的文本文件,但需要能够在设计时编辑。

找到了; 我可以在没有IsolatedStorage的情况下做到这一点,只需要使用这样构造的XML文件:

     Firece Game Hunting Developer : Sebastian Gray Deer (CC) : Martin Pettitt http://www.flickr.com/photos/mdpettitt    

然后像这样加载XML文件:

 public string LoadFromFile() { using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create("XMLFile1.xml")) { reader.MoveToContent(); reader.ReadToFollowing("credit"); credits = reader.ReadInnerXml(); } return credits; } 

可以将XML文件添加到普通项目(而不是内容项目)中,并将构建操作设置为“内容”,将“复制到输出目录”设置为“始终复制”。

我需要考虑使用IsolatedStorage吗?

是的,每个应用程序(OS应用程序除外)都需要使用IsolatedStorage将数据存储在物理内存中,或者您可以使用服务来处理数据。

IsolatedStorage示例

为什么不写内容管道扩展并让内容管理员担心呢? 这实际上非常简单。 这篇MSDN文章解释了如何 。

这是一篇博客文章 ,提供了出色的高级概述。