一、臨時應用資料概覽
臨時應用資料相當於網頁中緩衝,這些資料檔案是不能夠漫遊的,並 且隨時可以刪除。
通常系統為了維護任務可以隨時刪除掉這些臨時應用資料,同時我們也可以 通過“磁碟清理”將這些資料刪除掉。
一般我們在應用中儲存會話期間的臨時資訊,例如:QQ 的聊天紀錄等。
二、如何構建臨時應用資料
1、聲明臨時儲存物件
使用 ApplicationData.TemporaryFolder屬性擷取檔案。
Windows.Storage.StorageFolder temporaryFolder = ApplicationData.Current.TemporaryFolder;
2、將臨時資料寫入檔案
使用Windows.Storage.StorageFolder.CreateFileAsync和 Windows.Storage.FileIO.WriteTextAsync在臨時應用資料存放區中建立和更新檔案。
async void WriteTimestamp() { Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter = new Windows.Globalization.DatetimeFormatting.DateTimeFormatter("longtime"); StorageFile sampleFile = await temporaryFolder.CreateFileAsync("dataFile.txt", CreateCollisionOption.ReplaceExisting); await FileIO.WriteTextAsync(sampleFile, formatter.Format(DateTime.Now)); }
3、從檔案中擷取臨時資料
使用Windows.Storage.StorageFolder.GetFileAsync、 Windows.Storage.StorageFile.GetFileFromApplicationUriAsync 和 Windows.Storage.FileIO.ReadTextAsync在臨時應用資料存放區中開啟和讀取檔案。
async void ReadTimestamp() { try { StorageFile sampleFile = await temporaryFolder.GetFileAsync("dataFile.txt"); String timestamp = await FileIO.ReadTextAsync(sampleFile); } catch (Exception) { }}