[WindowsPhone] 隨貼即用 – 讀取 "獨立存儲" 中的文字資料
2013-10-17
簡述 : 讀取 獨立存儲(Isolated Storage) 中的文字資料
難度 : ★★
範例敘述: 本範例有一個按鈕,按下後讀取於([WindowsPhone] 隨貼即用 – 使用"獨立存儲" 儲存文字資料) 本文中儲存在獨立存儲(Isolated Storage) 的檔案 info.txt 中的內容
程式碼:
/// <summary>/// 傳出Isolated Storage 的資料 /// 如果沒有該檔案,或是讀取錯誤則回傳null/// </summary>/// <param name="filename"></param>/// <returns></returns>public static string ReadDataFromIsolatedStorage(string filename)
{IsolatedStorageFile storageFiles = IsolatedStorageFile.GetUserStoreForApplication();
StreamReader fileReader = null; //判斷檔案是否存在 if (storageFiles.FileExists(filename)) { try {fileReader = new StreamReader(new IsolatedStorageFileStream(filename, FileMode.Open, storageFiles));
var result = fileReader.ReadToEnd();
fileReader.Close();
return result;}
catch (Exception ex) {return null;
}
}
else {return null;
}
}
呼叫方法
private void btnWriteData2IsolatedStorage_Click(object sender, RoutedEventArgs e)
{ //WirteDataToIsolatedStorage("info.txt", "您好,我是當麻許"); MessageBox.Show(ReadDataFromIsolatedStorage("info.txt"));}
注意事項: 寫入 獨立存儲(Isolated Storage) 的方法請參考 ([WindowsPhone] 隨貼即用 – 使用"獨立存儲" 儲存文字資料 ,此篇文章為該篇延續
參考文件:
如何使用 Windows Phone 的獨立存儲資源管理器工具 http://msdn.microsoft.com/zh-cn/library/windowsphone/develop/hh286408(v=vs.105).aspx
System.IO.IsolatedStorage 命名空間 http://msdn.microsoft.com/zh-tw/library/system.io.isolatedstorage.aspx
下載源碼:
標籤:
隨貼即用
,
C#
,
WindowsPhone
-- Yesterday I wrote down the code. I bet I could be your hero. I am a mighty little programmer. 如果這篇文章有幫助到您,簡單留個言,或是幫我按個讚,讓我有寫下去的動力...
