2012-01-11 83 views
0

我有一些問題,隔離文件存儲,我想追加到一個文件,但是當我使用下面的代碼,我得到的參數無效的錯誤在這條線錯誤追加到IsolatedStorageFile

IsolatedStorageFileStream("Folder\\barcodeinfo.txt", FileMode.Append, 
            FileMode.OpenOrCreate, myStore)) 

我覺得它有什麼做的Filemode.Append ..我想追加到文件,而不是創造新的

// Obtain the virtual store for the application. 
IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication(); 
// Create a new folder and call it "MyFolder". 
myStore.CreateDirectory("Folder"); 

// Specify the file path and options. 
using (var isoFileStream = new IsolatedStorageFileStream("Folder\\barcodeinfo.txt", FileMode.Append, FileMode.OpenOrCreate, myStore)) 
{ 
     //Write the data 
     using (var isoFileWriter = new StreamWriter(isoFileStream)) 
     { 
      isoFileWriter.WriteLine(textBox1.Text); 
      isoFileWriter.WriteLine(textBox2.Text); 
      isoFileWriter.WriteLine(textBox3.Text); 
     } 
} 

回答

0

看起來你有FileMode.Append, FileMode.OpenOrCreate。這是2個文件模式。第一個參數是FileMode,第二個參數應該是FileAccess

這應該解決您的問題。

2

沒有超載需要兩個FileModes。它應該是

IsolatedStorageFileStream("Folder\\barcodeinfo.txt", FileMode.Append, 
            FileAccess.Write, myStore)); 

重要的事情需要注意FileMode.Append是:

[FileMode.Append]打開該文件,如果它存在,並尋求到該文件的末尾,或 創建一個新的文件。追加只能與Write一起使用。 試圖在文件結束之前嘗試查找某個位置將會拋出 IOException,並且嘗試讀取失敗並拋出NotOfficeException的 。

這就是爲什麼使用FileAccess.Write