Windows 8 Metro App開發[6]訪問Assets檔案夾

來源:互聯網
上載者:User

Windows.Storage名稱空間

我們首先需要認識一下Windows.Store名稱空間。

    如果你去閱讀微軟MSDN上提供的文檔Windows.Storage文檔,你會發現裡面有一個StorageFolder類,通過該類,我們可以操作檔案夾和相關的內容。StorageFolder類有一個方法叫做StorageFolder.CreateFileAsync,這個方法會在當前檔案夾中非同步建立一個檔案。同時,你也會看到,這裡有擷取檔案內容,讀取檔案屬性等方法。

作業碼如下:

 

   async void MainPage_Loaded(object sender, RoutedEventArgs e)        {            StorageFolder InstalltionFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;            string CountriesFile = @"Assets\output.xml";            StorageFile file = await InstalltionFolder.GetFileAsync(CountriesFile);            Stream countries = await file.OpenStreamForReadAsync();            XDocument doc = XDocument.Load(countries);               }

本地XML檔案:

 

<?xml version="1.0" encoding="utf-8" ?><bookstore><book category="CHLDREN">  <title>Harry Potter</title>  <author>JK. Rowling</author>  <year>2005</year>  <price>39.95</price></book><book category="WEB">  <title>Harry Potter</title>  <author>JK. Rowling</author>  <year>2005</year>  <price>39.95</price></book></bookstore>

 

這裡需要特別注意的是async和await兩個關鍵字,這兩個是C#中新的關鍵字,在這裡我們只需要知道在調用非同步函數的時候需要使用到,後續的學習系列中我會對async和await進行詳細的講解。

運行效果:

 

 

 

相關文章

聯繫我們

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