0

我想使用 BinaryReader 读取二进制文件,但我不断收到异常:

using (var stream = File.Open("file.bin", FileMode.Open, FileAccess.Read))
        {
            using (BinaryReader r = new BinaryReader(stream)) //EXCEPTION
            {

            }
        }

“file.bin”已在构建操作中设置为内容,但我不断收到此异常:

System.MethodAccessException 未处理

尝试访问方法失败:System.IO.File.Open(System.String, System.IO.FileMode, System.IO.FileAccess)

4

1 回答 1

1

您不能File.Open在 Windows Phone 7 上使用 - 您必须使用独立存储

有关更多详细信息,请参见System.IO.IsolatedStorage命名空间。

例如:

using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (var stream = store.OpenFile("file.bin", FileMode.Open))
    {
        using (var reader = new BinaryReader(stream))
        {

        }
    }
}

编辑:如评论中所述,对于 XAP 中内置的内容,您应该使用Application.GetResourceStream.

于 2011-11-25T08:04:32.053 回答