日本好好热aⅴ|国产99视频精品免费观看|日本成人aV在线|久热香蕉国产在线

  • <cite id="ikgdy"><table id="ikgdy"></table></cite>
    1. 西西軟件園多重安全檢測下載網(wǎng)站、值得信賴的軟件下載站!
      軟件
      軟件
      文章
      搜索

      首頁編程開發(fā)其它知識 → WP7&WP8開發(fā)、文件讀寫操作上的區(qū)別

      WP7&WP8開發(fā)、文件讀寫操作上的區(qū)別

      相關(guān)軟件相關(guān)文章發(fā)表評論 來源:西西整理時(shí)間:2013/6/8 15:08:17字體大小:A-A+

      作者:西西點(diǎn)擊:34次評論:0次標(biāo)簽: 文件操作

      • 類型:游戲其他大。197KB語言:英文 評分:3.3
      • 標(biāo)簽:
      立即下載

      一、WP7的文件操作:

      如果在wp7平臺上去寫入一個(gè)文件,我們會使用: IsolatedStorageFile

      代碼如下:

      ①寫入文件
      private void WriteFile(string fileName, string content)
      {
      using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
      {
      using (IsolatedStorageFileStream isolatedStorageFileStream = isolatedStorageFile.CreateFile(fileName))
      {
      using (StreamWriter streamWriter = new StreamWriter(isolatedStorageFileStream))
      {
      streamWriter.Write(content);
      }
      }
      }
      }
      ②讀取文件
      private string ReadFile(string fileName)
      {
      string text;
      using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
      {
      using (IsolatedStorageFileStream isolatedStorageFileStream = isolatedStorageFile.OpenFile(fileName, FileMode.Open))
      {
      using (StreamReader streamReader = new StreamReader(isolatedStorageFileStream))
      {
      text = streamReader.ReadToEnd();
      }
      }
      }
      return text;
      }
      二、WP8文件操作

      wp8與win8的文件操作方式類似,如果你在win8下寫過文件操作,那么WP8自然熟悉,這需要用到2個(gè)接口:IStorageFile和 IStorageFolder, 可以看出,一個(gè)是對文件的操作,一個(gè)是對目錄的。

      這兩個(gè)接口均在 :Windwos.Storage組件中,

      注意:因在windows 8 開發(fā)中大量使用了WinRT異步編程方式,故在WP8中編程亦如此。

      代碼如下:

      ①寫入文件:

      public async Task WriteFile(string fileName, string text)
      {
      IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
      IStorageFile storageFile = await applicationFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
      using (Stream stream = await storageFile.OpenStreamForWriteAsync())
      {
      byte[] content = Encoding.UTF8.GetBytes(text);
      await stream.WriteAsync(content, 0, content.Length);
      }
      }
      ②讀取文件:

      public async Task<string> ReadFile(string fileName)
      {
      string text;
      IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
      IStorageFile storageFile = await applicationFolder.GetFileAsync(fileName);
      IRandomAccessStream accessStream = await storageFile.OpenReadAsync();
      using (Stream stream = accessStream.AsStreamForRead((int)accessStream.Size))
      {
      byte[] content = new byte[stream.Length];
      await stream.ReadAsync(content, 0, (int) stream.Length);
      text = Encoding.UTF8.GetString(content, 0, content.Length);
      }
      return text;
      }
      三、兼容性問題

      上面的代碼分別對WP7與WP8平臺的文件寫入與讀取操作進(jìn)行了簡單的說明

      那么在WP7應(yīng)用中的文件目錄結(jié)構(gòu)定義,是不是在WP8中也是一樣的呢?

      其實(shí),兩者是有點(diǎn)區(qū)別的,這點(diǎn)區(qū)別,也是日后WP7向WP8遷移過程中必須要考慮的元素之一。

      在WP7中,創(chuàng)建文件test.txt,那么,它在隔離存儲區(qū)的文件目錄結(jié)構(gòu)如下:

      C:\Data\Users\DefaultAppAccount\AppData\{ProductID}\Local\IsolatedStore\test.txt

      在WP8中,創(chuàng)建相同文件,文件存儲目錄結(jié)構(gòu)如下:

      C:\Data\Users\DefaultAppAccount\AppData\{ProductID}\Local\test.txt

      兩者一對比,我們發(fā)現(xiàn):在WP8開發(fā)中,如果使用現(xiàn)有的WP7代碼時(shí),如果你不想改變原有文件目錄結(jié)構(gòu),那你就必須要首先獲取WP7隔離存儲目錄.即:

      IStorageFolder applicationFolder =await ApplicationData.Current.LocalFolder.GetFolderAsync("IsolatedStore");
      最后,想說明的是:

      如果你想“偷懶”或者說減少開發(fā)工作量,就看看我上面說的這些

      但如果打算做WP8/WIN8兩個(gè)平臺,最好全新開發(fā)基于WP8平臺的應(yīng)用,這可以加快你的Win8應(yīng)用的移植。

        相關(guān)評論

        閱讀本文后您有什么感想? 已有人給出評價(jià)!

        • 8 喜歡喜歡
        • 3 頂
        • 1 難過難過
        • 5 囧
        • 3 圍觀圍觀
        • 2 無聊無聊

        熱門評論

        最新評論

        發(fā)表評論 查看所有評論(0)

        昵稱:
        表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
        字?jǐn)?shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)