当我尝试在WP7应用程序中打开文件时:
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); IsolatedStorageFileStream nameXmlFile = new IsolatedStorageFileStream("Names.xml",System.IO.FileMode.Open,isf);
我收到以下错误:
Operation not permitted on IsolatedStorageFileStream.
我不确定为什么它没有打开,因为我在我的应用程序的其他地方使用了确切的代码,它工作正常.
关于为什么会发生这种情况的任何线索?
编辑
我使用以下代码将文件添加到App.xaml.cs Application_Launching事件中的独立存储:
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); IsolatedStorageFileStream feedXmlFile = new IsolatedStorageFileStream("Names.xml",System.IO.FileMode.Create,isf);
解决方法
使用IsolatedStorageFileStream构造函数的一个问题是生成的异常信息有限.替代的OpenFile方法有一组更丰富的例外.
作为一般的经验法则,如果API允许您使用构造函数或方法执行相同的操作,请使用该方法.在这种情况下,请尝试使用此代码: –
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); IsolatedStorageFileStream nameXmlFile = isf.OpenFile("Names.xml",System.IO.FileMode.Open);
如果失败,您将至少缩小潜在原因.
这看起来很明显但是在您创建的代码中,您实际上已经写入并关闭了您创建的文件?