1

我在使用 Windows Phone 8 和 MonoGame 框架读取包含我的关卡信息的简单文本文件时遇到问题。

我的文件读取功能适用于普通的 Windows Phone 8 项目,但是当我尝试在 monogame 项目中使用它时,它在尝试创建新的 FileStream 时给了我这个错误:

“在 mscorlib.ni.dll 中发生了‘System.MethodAccessException’类型的异常,但未在用户代码中处理”

这是我的文件读取功能

private string readFile(string fileName)
{
    FileStream fs = new FileStream(fileName, FileMode.Open);

    byte[] bytes = new byte[fs.Length];
    int numBytesToRead = (int)fs.Length;
    int numBytesRead = 0;
    while (numBytesToRead > 0)
    {
        int n = fs.Read(bytes, numBytesRead, numBytesToRead);

        if (n == 0)
        {
            break;
        }

        numBytesToRead -= n;
        numBytesRead += n;
    }

    numBytesToRead = bytes.Length;
    return System.Text.UTF8Encoding.UTF8.GetString(bytes, 0, bytes.Length);
}

我的方法完全错误还是有人知道为什么这不起作用?我正在尝试从我的项目文件中读取文件。

4

1 回答 1

0

Since Windows Phone apps are sandboxed, you would typically use the Isolated Storage classes for saving files, and not go directly to System.IO. But since you mention level information (compiled into your app?), perhaps the following link will help:

How do I embed and read a text file ina WP7 app?

于 2013-01-24T19:40:13.207 回答