我在创建文件后打开文件时出错
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") });
}
}
答案 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()
方法)