IsolatedStorageFileStream上不允许操作

时间:2012-09-25 14:12:00

标签: windows-phone-7 windows-phone-7.1 windows-phone windows-phone-7.1.1

我在创建文件后打开文件时出错

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 个答案:

答案 0 :(得分:4)

这是因为你没有关闭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()方法)

相关问题