Windows 8風格應用開發入門 二十八 臨時應用資料

來源:互聯網
上載者:User

一、臨時應用資料概覽

臨時應用資料相當於網頁中緩衝,這些資料檔案是不能夠漫遊的,並 且隨時可以刪除。

通常系統為了維護任務可以隨時刪除掉這些臨時應用資料,同時我們也可以 通過“磁碟清理”將這些資料刪除掉。

一般我們在應用中儲存會話期間的臨時資訊,例如: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)      {   }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.