如何解决Operation not permitted on IsolatedStorageFileStream错误
当保存图片文件到独立存储时,报“Operation not permitted on IsolatedStorageFileStream.”的异常,代码如下:
using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(
ImageFileName,
FileMode.Create,
IsolatedStorageFile.GetUserStoreForApplication())
)
{
long imgLen = e1.Result.Length;
byte[] b = new byte[imgLen];
e1.Result.Read(b, 0, b.Length);
isfs.Write(b, 0, b.Length);
isfs.Flush();
}
相对进行曲
11 years, 10 months ago
Answers
具体可以参考 微软文档
这实际是一个文件被锁的问题。当你的代码执行多次,因为执行速度的关系,外部存储遭遇锁定状态。因此,你必须定义好文件读写模式。比如:
using (var isoStream = new IsolatedStorageFileStream("Notes.xml", FileMode.Open, FileAccess.Read, FileShare.Read, isolatedStorage)
{
//...
}
如果你想在其它进程读文件的同时,来写这个文件,那么,你必须做同步。比如:
private readonly object _readLock = new object();
lock(_readLock)
{
using (var isoStream = new IsolatedStorageFileStream("Notes.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, isolatedStorage)
{
//...
}
}
微雨洒庭轩
answered 10 years, 3 months ago