在IsolatedStorageFileStream上不允许进行该操作 [英] Operation not permitted on IsolatedStorageFileStream

查看:96
本文介绍了在IsolatedStorageFileStream上不允许进行该操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建文件后打开文件时出现错误

I get an error when I open the file after it is created

using (var myFileStore = IsolatedStorageFile.GetUserStoreForApplication())
        {
            myFileStore.CreateFile(DateTime.Now.Ticks + ".txt");
        }
using (var myFileStore = IsolatedStorageFile.GetUserStoreForApplication())
        {
            temp = myFileStore.GetFileNames();
            for (int k = 0; k < temp.Length; k++)
            {
                IsolatedStorageFileStream file1 = myFileStore.OpenFile(temp[k], FileMode.Open, FileAccess.Read);
                dataSource.Add(new SampleData() { Name = temp[k], Size = Convert.ToString(Math.Round(Convert.ToDouble(file1.Length) / 1024 / 1024, 1) + " MB") });
            }
        }

推荐答案

那是因为您没有关闭CreateFile方法返回的流!

That is due to the fact you didn't close the stream returned by the CreateFile method!

您的代码应如下所示:

using (var myFileStore = IsolatedStorageFile.GetUserStoreForApplication())
{
    myFileStore.CreateFile(DateTime.Now.Ticks + ".txt").Dispose();
}

using (var myFileStore = IsolatedStorageFile.GetUserStoreForApplication())
{
    using(myFileStore.CreateFile(DateTime.Now.Ticks + ".txt"))
    {
    }
}

与下面的OpenFile中的内容相同.

And the same in the OpenFile below.

最底线,您应该始终处置流(通过使用using子句或Dispose()方法)

Bottom line you should always dispose your stream (by using the using clause or Dispose() method)

这篇关于在IsolatedStorageFileStream上不允许进行该操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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