一、臨時應用數據概覽
臨時應用數據相當於網頁中緩存,這些數據文件是不能夠漫游的,並 且隨時可以刪除。
通常系統為了維護任務可以隨時刪除掉這些臨時應用數據,同時我們也可以 通過“磁盤清理”將這些數據刪除掉。
一般我們在應用中存儲會話期間的臨時信息,例如: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)
{
}
}