Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IsolatedStorageFileStream exception is throw when file is opened?

when I attempt to open a file in a WP7 app:

IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream nameXmlFile = new IsolatedStorageFileStream("Names.xml", System.IO.FileMode.Open, isf);

I recieve the following error:

Operation not permitted on IsolatedStorageFileStream.

I'm not sure why it isn't opening because I used the exact code somewhere else in my app and it works fine. Any leads as to why this is happening?

EDIT

I used the following code to add the file to isolated storage in the App.xaml.cs Application_Launching Event:

IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream feedXmlFile = new IsolatedStorageFileStream("Names.xml",System.IO.FileMode.Create, isf);
like image 327
Edward Avatar asked Dec 22 '10 18:12

Edward


3 Answers

One of the problems with using the IsolatedStorageFileStream constructor is that the exception generated has limited info. The alternative OpenFile method has a richer set of exceptions.

As a general rule of thumb if an API allows you to do the same thing with a constructor or with a method go with the method. In this case try this code instead:-

IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream nameXmlFile = isf.OpenFile("Names.xml", System.IO.FileMode.Open);

If this fails you will have at least narrowed down the potential cause.

This may seem obvious but in your creation code you have actually written to and closed the file you created?

like image 100
AnthonyWJones Avatar answered Nov 16 '22 23:11

AnthonyWJones


IsolatedStorage Exception is a known issue whil fires Application_Launching. more details

like image 2
Lukasz Madon Avatar answered Nov 16 '22 23:11

Lukasz Madon


When you run into an exception during file access, check for two things:

  • isolated storage is not thread safe, so use a 'lock' when accessing the file system Isolated storage best practices
  • Fully read the stream into memory before closing the stream. So don't pass the filestream back to for example an image control. Reading a bitmap stream from isolated storage
like image 2
markwilde Avatar answered Nov 16 '22 23:11

markwilde