23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

• The Windows.Storage.DownloadsFolder object provides a createFolderAsync method through<br />

which you can obtain a StorageFolder in that location. It also provides a createFileAsync<br />

method to create a StorageFile directly. You would use this API if your app manages<br />

downloaded files directly. Note that DownloadsFolder itself provides only these two methods; it<br />

is not a StorageFolder in its own right.<br />

• The static method Windows.Storage.StorageFolder.getFolderFromPathAsync returns a<br />

StorageFolder for a given pathname if and only if your app already has permissions to access it;<br />

otherwise, you’ll get an Access Denied exception. A similar static method exists for files called<br />

Windows.Storage.StorageFile.getFileFromPathAsync, with the same restrictions;<br />

Windows.Storage.StorageFile.getFileFromApplicationUriAsync opens files with<br />

ms-appx:// (package) and ms-appdata:/// URIs. Other schemas are not supported.<br />

• Once a folder or file object is obtained, it can be stored in the Windows.Storage.-AccessCache<br />

that allows an app to retrieve it sometime in the future with the same programmatic<br />

permissions. This is primarily needed for folders or files selected through the pickers because<br />

permission to access the storage item is granted only for the lifetime of that in-memory object.<br />

You should always use this API, as demonstrated in Scenario 6 of the File access sample, where<br />

you’d normally think to save a file path. Again, StorageFolder.getFolder-FromPathAsync and<br />

StorageFile.getFileFromPathAsync will throw Access Denied exceptions if they refer to any<br />

locations where you don’t already have permissions. Pathnames also will not work for files<br />

provided by another app through the file picker, because the StorageFile object might not, in<br />

fact, refer to anything that actually exists on the file system.<br />

Once you have a StorageFolder in hand, you can do the kinds of operations you’d expect: obtain<br />

folder properties (including a thumbnail), create and/or open files and folders, and enumerate the<br />

folder’s contents. With the latter, the API provides for obtaining a list folder contents, of course (see the<br />

getItemsAsync method), but what you want more often is a partial list of those contents according to<br />

certain criteria, along with thumbnails and other indexed file metadata (music album and track info,<br />

picture titles and tags, etc.) that you can use to group and organize the files. This is the purpose of file,<br />

folder, and item (file + folder) queries, which you manage through the methods<br />

createFileQuery-[WithOptions], createFolderQuery[WithOptions], and<br />

createItemQuery[WithOptions]. We already saw a little of this with the FlipView app we built using the<br />

Pictures Library in Chapter 5, “Collection and Collection Controls” and we’ll return to the subject in the<br />

context of user data, the primary scenario for queries, at the end of this chapter. 41<br />

41 Obtaining folder properties happens through a storage item’s getBasicPropertiesAsync method (linked here for<br />

StorageFolder but also available on StorageFile.) This provides a<br />

Windows.Storage.FileProperties.-BasicPropertiesClass, which then has a retrievePropertiesAsync method.<br />

Through this you can retrieve any number of Windows properties. A property like System.FreeSpace will actually give you<br />

the free space on the drive where the StorageFolder lives.<br />

322

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!