Operation not permitted on IsolatedStorageFileStream

我与影子孤独终老i 提交于 2020-01-04 02:40:08

问题


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

回答1:


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

Your code should look like this:

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

or

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

And the same in the OpenFile below.

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



来源:https://stackoverflow.com/questions/12584813/operation-not-permitted-on-isolatedstoragefilestream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!