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 le开发者_开发知识库ads 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);
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?
IsolatedStorage Exception is a known issue whil fires Application_Launching. more details
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
精彩评论