3

我得到一個錯誤,當我打開文件創建後,不允許的操作上IsolatedStorageFileStream

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

回答

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()方法)

相關問題