Skip to content

Commit 9482a1d

Browse files
committed
Add examples to WinRT page
1 parent b1985cd commit 9482a1d

File tree

1 file changed

+84
-3
lines changed

1 file changed

+84
-3
lines changed

hub/apps/develop/files/winrt-files.md

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,94 @@ ms.localizationpriority: medium
99
---
1010
# Access files and folders with Windows App SDK and WinRT APIs
1111

12-
Packaged Windows App SDK apps can leverage WinRT APIs for reading and writing app settings, file and folder pickers, and special sand-boxed locations such as the Video/Music library. Additionally, any packaged desktop app can utilize both WinRT and Win32 APIs in the Windows SDK, as well as the APIs provided in the .NET SDK. This article provides guidance on how to use the WinRT storage APIs to query files and folders, retrieve file properties, and work with the **Pictures** library.
12+
Packaged Windows App SDK apps can leverage [WinRT APIs](/uwp/api/) for reading and writing app settings, file and folder pickers, and special sand-boxed locations such as the Video/Music library. Additionally, any packaged desktop app can utilize both WinRT and Win32 APIs in the Windows SDK, as well as the APIs provided in the .NET SDK. This article provides guidance on how to use the WinRT storage APIs to query files and folders, retrieve file properties, and work with the **Pictures** library.
1313

1414
## Query files and folders
1515

16-
## Get file properties
16+
The following example shows how to use the [StorageFolder](/uwp/api/windows.storage.storagefolder) and [StorageFile](/uwp/api/windows.storage.storagefile) APIs to query the **Documents** library for files and folders. The example uses the `GetFilesInFolderAsync` method to recursively iterate through the folder structure and append the file names to a `StringBuilder` object.
1717

18-
## Files and folders in the Pictures library
18+
```csharp
19+
using Windows.Storage;
20+
...
21+
private async Task<string> GetDocumentsContentsAsync()
22+
{
23+
StorageFolder docsFolder = KnownFolders.DocumentsLibrary;
24+
StringBuilder outputBuilder = new();
25+
await GetFilesInFolderAsync(docsFolder, outputBuilder);
26+
27+
return outputBuilder.ToString();
28+
}
29+
30+
private async Task GetFilesInFolderAsync(StorageFolder folder, StringBuilder outputBuilder)
31+
{
32+
IReadOnlyList<IStorageItem> storageItem = await folder.GetItemsAsync();
33+
34+
foreach (var item in storageItem)
35+
{
36+
if (item is StorageFolder)
37+
{
38+
await GetFilesInFolderAsync(item as StorageFolder, outputBuilder);
39+
}
40+
else
41+
{
42+
outputBuilder.AppendLine($"Found {item.Name} in folder {folder.Name}");
43+
}
44+
}
45+
}
46+
```
47+
48+
## Get basic file properties
49+
50+
The following example takes the `GetFilesInFolderAsync` method from the previous example and adds the ability to retrieve the file size and date modified for each file. The example uses the [BasicProperties](/uwp/api/windows.storage.fileproperties.basicproperties) API to retrieve the file size and date modified for each file, formats the file size, and appends the size and date modified to the `StringBuilder` object after each file and folder name.
51+
52+
```csharp
53+
using Windows.Storage;
54+
using Windows.Storage.FileProperties;
55+
...
56+
private async Task GetFilesInFolderAsync(StorageFolder folder, StringBuilder outputBuilder)
57+
{
58+
IReadOnlyList<IStorageItem> storageItem = await folder.GetItemsAsync();
59+
60+
foreach (var item in storageItem)
61+
{
62+
if (item is StorageFolder)
63+
{
64+
await GetFilesInFolderAsync(item as StorageFolder, outputBuilder);
65+
}
66+
else
67+
{
68+
outputBuilder.AppendLine($"Found {item.Name} in folder {folder.Name}");
69+
70+
// Append each file's size and date modified.
71+
BasicProperties basicProperties = await item.GetBasicPropertiesAsync();
72+
string fileSize = string.Format("{0:n0}", basicProperties.Size);
73+
outputBuilder.AppendLine($" - File size: {fileSize} bytes");
74+
outputBuilder.AppendLine($" - Date modified: {basicProperties.DateModified}");
75+
}
76+
}
77+
}
78+
```
79+
80+
## Working with the Pictures library
81+
82+
In this example, the app is configured to receive notifications when the **Pictures** library is updated. The example uses the [StorageLibrary](/uwp/api/windows.storage.storagelibrary) API to retrieve the **Pictures** library and the [DefinitionChanged](/uwp/api/windows.storage.storagelibrary.definitionchanged) event to receive notifications when the library is updated. The `DefinitionChanged` event is invoked when the list of folders in the current library changes. The example uses the library's `Folders` property to iterate through the folders in the **Pictures** library and writes the folder name to the console.
83+
84+
```csharp
85+
using Windows.Storage;
86+
...
87+
private async Task Configure()
88+
{
89+
StorageLibrary picturesFolder = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);
90+
picturesFolder.DefinitionChanged += picturesFolder_DefinitionChanged;
91+
}
92+
private void picturesFolder_DefinitionChanged(StorageLibrary sender, object args)
93+
{
94+
foreach (StorageFolder item in sender.Folders)
95+
{
96+
Console.WriteLine($"Folder {item.Name} found.");
97+
}
98+
}
99+
```
19100

20101
## See also
21102

0 commit comments

Comments
 (0)