Quickstart: Accessing files programmatically (XAML)
Access files and folders that are in a location like a folder, library, device, or network location. You can also query the files and folders in a location by constructing file and folder queries.
Roadmap: How does this topic relate to others? See:
- Roadmap for Windows Runtime apps using C# or Visual Basic
- Roadmap for Windows Runtime apps using C++
Prerequisites
-
Understand async programming for Windows Runtime apps using C++, C#, or Visual Basic
You can learn how to write asynchronous apps in C# or Visual Basic, see Quickstart: Calling asynchronous APIs in C# or Visual Basic. To learn how to write asynchronous apps in C++, see Asynchronous Programming in C++.
-
Access permissions to the location
For example, the code in these examples require the Pictures library capability, but your location may require a different capability or no capability at all. To learn more, see File access and permissions for Windows Store apps. To learn about how to use the file picker to access files and folders, see Quickstart: Accessing files with Windows Store apps.
Enumerate files and folders in a location
The following example uses the StorageFolder.GetFilesAsync method to get all the files in the PicturesLibrary and then lists the name of each file. Next, the example uses the GetFoldersAsync method to get all the folders in the PicturesLibrary and then lists the name of each folder.
Alternatively, you can use the GetItemsAsync method to get both the files and the folders in a particular location. The following example uses the GetItemsAsync method to get all files and folders in the PicturesLibrary. Then the example lists the name of each file or folder. If the item is a folder, the example appends "folder" to the name.
Query files in a location and enumerate matching files
The following example gets the files in the PicturesLibrary and groups them by the month. The example creates a StorageFolderQueryResult object by calling StorageFolder.CreateFolderQuery and passing the CommonFolderQuery.GroupByMonth value to the method. Then the example calls StorageFolderQueryResult.GetFoldersAsync. The results of GetFoldersAsync are virtual folders (StorageFolder objects) that are based on the CommonFolderQuery value, with the files in the PicturesLibrary and its sub-folders grouped into the virtual folders.
The output of the example looks similar to the following.
February 2012 (2) MyImage3.png MyImage4.png November 2011 (2) MyImage1.png MyImage2.png
Summary and next steps
To learn about reading and writing files, see Quickstart: Reading and writing a file and the File access sample. To learn about working with image files, see Quickstart: Image and ImageBrush, Quickstart: Imaging, and the XAML image sample.
To learn how to access files through the file picker, see Quickstart: Accessing files with file pickers.
Related topics
- Accessing data and files
- Quickstart: Accessing files with file pickers
- Quickstart: Reading and writing a file
- File access and permissions
- Programmatic file search sample
- Folder enumeration sample
- File access sample
- XAML image sample
- Reference
- Windows.Storage.KnownFolders class
- Windows.Storage.StorageFile class
- Windows.Storage.StorageFolder class
- Windows.Storage.StorageItemTypes enum
- Windows.Storage.Search.CommonFolderQuery enum
- Windows.Storage.Search.StorageFolderQueryResult class