打开文件时抛出IsolatedStorageFileStream异常? [英] IsolatedStorageFileStream exception is throw when file is opened?

查看:24
本文介绍了打开文件时抛出IsolatedStorageFileStream异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在 WP7 应用程序中打开文件时:

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

我收到以下错误:

不允许对独立存储文件流进行操作.

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?

编辑

我使用以下代码将文件添加到 App.xaml.cs Application_Launching 事件中的独立存储:

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

推荐答案

使用 IsolatedStorageFileStream 构造函数的问题之一是生成的异常信息有限.替代的 OpenFile 方法具有更丰富的异常集.

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.

作为一般的经验法则,如果 API 允许您使用构造函数或使用方法执行相同的操作.在这种情况下,请尝试使用以下代码:-

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?

这篇关于打开文件时抛出IsolatedStorageFileStream异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆