不允许操作上IsolatedStorageFileStream(Operation not perm

2019-09-23 00:03发布

我得到一个错误,当我打开文件创建后,

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

Answer 1:

那是因为你没有关闭由返回流的事实CreateFile方法!

您的代码应该是这样的:

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相同。

底线你应该总是处置您的流(通过using条款或Dispose()的方法)



文章来源: Operation not permitted on IsolatedStorageFileStream