Windows Phone 8 新增功能:Windows.Storage新的檔案操作類型

來源:互聯網
上載者:User

為了與Windows 8 的統一, Windows phone 8提供了新的檔案操作類型,新的檔案操作類型都包含在Windows.Storage命名空間中,其中包括StorageFolder,StorageFile,FileIO等類庫。Windwos.Storage組件中主要包含了IStorageFile和 IStorageFolder, 可以看出,一個是對檔案的操作,一個是對檔案夾的。

在正式版的SDK中,windows phone 7 通過

 IsolatedStorageFile.GetUserStoreForApplication(); 查看私人變數 m_AppFilesPathAccess 可知

和 Windows phone 8 通過

await ApplicationData.Current.LocalFolder

擷取 的檔案目錄結構定義是一致的。都是

C:\Data\Users\DefApps\AppData\{ProductID}\Local

差異的是wp8對檔案操作基本都是基於非同步架構,而wp7主要是使用的是同步方法。

使用IStorageFile建立一個檔案

        public void IStorageFileCreateFile(string fileName)        {            IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;            IAsyncOperation<StorageFile> iaStorageFile = applicationFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);            iaStorageFile.Completed = (IAsyncOperation<StorageFile> asyncInfo, AsyncStatus asyncStatus) =>            {                if (asyncStatus == AsyncStatus.Completed)                {                }            };        }

使用IStorageFile讀取一個檔案

        public async Task<string> IStorageFileReadFile(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;        }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.