【问题标题】:Reading Binary file on Windows phone在 Windows 手机上读取二进制文件
【发布时间】:2011-11-25 08:00:33
【问题描述】:

我想使用 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)

【问题讨论】:

    标签: windows-phone-7 binaryfiles binaryreader


    【解决方案1】:

    您不要在 Windows Phone 7 上使用 File.Open - 您必须使用 isolated storage

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

    例如:

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

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

    【讨论】:

    • 但我需要从 xap 文件的内容中读取文件。我可以使用 IsolatedStroage 访问这些文件吗?
    • @user836252:啊,你应该说。不,那些不会在隔离存储中。我相信你需要Application.GetResourceStream。见this similar question
    • 我已经说过它已被设置为构建操作中的内容,哈哈,但是谢谢,它可以使用 GetResourceStrea
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    • 2019-08-27
    • 2017-10-01
    • 2011-09-03
    • 2016-01-15
    相关资源
    最近更新 更多