0

I want to read a binary file using BinaryReader, but I keep getting an exception:

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

            }
        }

the "file.bin" has been set as a Content in the build action, but I keep getting this exception:

System.MethodAccessException was unhandled

Attempt to access the method failed: System.IO.File.Open(System.String, System.IO.FileMode, System.IO.FileAccess)

1 Answer 1

1

You don't use File.Open on Windows Phone 7 - you have to use isolated storage.

See the System.IO.IsolatedStorage namespace for more details.

For example:

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

        }
    }
}

EDIT: As noted in comments, for content built into the XAP, you should use Application.GetResourceStream.

Sign up to request clarification or add additional context in comments.

3 Comments

but I need to read a file from the content of my xap file. can i access these files using IsolatedStroage?
@user836252: Ah, you should have said. No, those won't be in isolated storage. I believe you need Application.GetResourceStream. See this similar question.
I've said it has been set as a content in the build action lol but thanks, it works using GetResourceStrea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.