Windows Phone 7的solatedStorage可以用來保存應用程序的數據和設置。結構圖如下
?
一、相關類
1.IsolatedStorageFile類
1)描述:表示在獨立存儲空間中的文件和目錄。
2)重要屬性
long AvailableFreeSpace:IsolatedStorage有效的剩余空間。
long Quota:IsolatedStorage的總容量。
3)重要方法
void CreateDirectory(string dir):在IsolatedStorage中創建一個指定的目錄。
IsolatedStorageFileStream CreateFile(string path):在IsolatedStorage中創建一個文件。
void DeleteDirectory(string dir):刪除IsolatedStorage的指定目錄。
void DeleteFile(string file):刪除IsolatedStorage的指定文件。
bool DirectoryExists(string path):判斷IsolatedStorage中是否存在指定的目錄,如果存在則返回true。
?bool FileExists(string path):判斷IsolatedStorage中是否存在指定的文件,如果存在則返回true。
string[] GetDirectoryNames():獲取IsolatedStorage中的所有目錄名稱。
string[] GetFileNames():獲取IsolatedStorage中的所有文件名稱。
static IsolatedStorageFile GetUserStoreForApplication():由一個應用程序調用,獲得用戶范圍可以使用的獨立存儲。
IsolatedStorageFileStream OpenFile(string path, FileMode mode):打開一個指定的文件。
2.IsolatedStorageFileStream類
1)描述:打開一個文件流。
二、應用
1.創建并寫入文件
首先我們獲取空間
var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
保存的文件名
??????????? string fileName = "simple.txt";
打開流
??????????? using (var file=appStorage.OpenFile(fileName,System.IO.FileMode.OpenOrCreate,System.IO.FileAccess.Write))
??????????? {
??????????????? using (var writer=new System.IO.StreamWriter(file))
??????????????? {
寫入數據
??????????????????? writer.Write(this.inputInfo.Text);
??????????????? }
??????????? }
2.打開并讀取文件
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
??????????? {
??????????????? using (StreamReader reader=new StreamReader(store.OpenFile("simple.txt",FileMode.Open,FileAccess.Read)))
??????????????? {
??????????????????? inputInfo.Text = reader.ReadToEnd();
??????????????? }
??????????? }
?
三、源碼下載
點擊這里下載示例源碼