0

这个问题与此有些相似

就我而言,我有一个文本文件。而且由于没有适用于文本文件的内容导入器,我必须使用流阅读器编写自己的函数。我想要完成的是从文本文件中读取,并在选项屏幕中相应地设置一些值。我已经添加了所有必要的引用,但是使用“../options.txt”作为文件路径不起作用。文件路径很可能被解析为 Content 文件夹以外的其他内容。那我该如何进行呢?

此外,我收到错误消息说“尝试访问方法(System.IO ....ctor)失败”。我是否缺少添加其他参考?

4

1 回答 1

0

这必须在 xBox 360 上工作吗?如果是这样,则无法像在 Windows 中那样仅使用文件系统,则需要使用存储设备。老实说,无论如何我都会使用这种方法,所以如果你决定将你的游戏带到 WP7 或 360 上,它就可以工作。

有一个很好的演示游戏,可以在这个Microsoft 页面上加载和保存数据以及它在做什么的描述

这是从demo中截取的,它展示了如何打开一个存储容器并将一些配置数据序列化到它,加载操作同样简单。

// Create the data to save.
SaveGameData data = new SaveGameData();
data.PlayerName = "Hiro";
data.AvatarPosition = new Vector2(360, 360);
data.Level = 11;
data.Score = 4200;

// Open a storage container.
IAsyncResult result =
    device.BeginOpenContainer("StorageDemo", null, null);

// Wait for the WaitHandle to become signaled.
result.AsyncWaitHandle.WaitOne();

StorageContainer container = device.EndOpenContainer(result);

// Close the wait handle.
result.AsyncWaitHandle.Close();

string filename = "savegame.sav";

// Check to see whether the save exists.
if (container.FileExists(filename))
    // Delete it so that we can create one fresh.
    container.DeleteFile(filename);

// Create the file.
Stream stream = container.CreateFile(filename);

// Convert the object to XML data and put it in the stream.
XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData));
serializer.Serialize(stream, data);

// Close the file.
stream.Close();

// Dispose the container, to commit changes.
container.Dispose();
于 2012-06-14T07:45:48.120 回答