[windows phone 開發] IsolatedStorageFile.OpenFile() 使用心得

[windows phone 開發] IsolatedStorageFile.OpenFile() 使用心得

前言


最近開始開發一些WP8 App

要建立一個簡單的Local DataBase

所以對IsolatedStorageFile.OpenFile()做了一番研究

 

實作


 

1:  IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication();
2:  IsolatedStorageFileStream isoStream = isoFile.OpenFile("data.txt", FileMode.Create, FileAccess.Write);
3:  StreamWriter writer = new StreamWriter(isoStream);

 


		

 

 

 

接著就可以利用writer對data.txt進行寫入的動作

這邊可以看到 OpenFile主要有三個屬性:OpenFile(String, FileMode, FileAccess)

 

(1) String : 檔案名稱

 

(2) FileMode : 指定模式來開啟檔案

成員名稱 說明
Append 在檔案存在時開啟它並搜尋至檔案末端,或建立新檔案。
CreateNew 指定作業系統應該建立新檔案。
Open 指定作業系統應該開啟現有的檔案。
OpenOrCreate 指定作業系統,如果檔案存在應該開啟檔案,否則建立新的檔案。

 

(3) FileAccess:指定讀取/寫入權限

成員名稱 說明
Read 檔案的讀取權限,資料可從檔案讀取
Write 檔案的寫入權限,資料可寫入檔案
ReadWrite 讀取和寫入檔案的權限

 

P.S. 個人覺得FileMode.Append很好用

因為能從檔案最後面開始寫是很重要的

若使用單純的FileMode.Open來開啟檔案

寫入時會把之前的資料都覆蓋掉喔~