IsolatedStorageFileStream exception is throw when file is opened?

半世苍凉 提交于 2020-01-23 06:27:06

问题


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);

回答1:


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?




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/4512165/isolatedstoragefilestream-exception-is-throw-when-file-is-opened

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!