Skip to content

Commit 2cb0c8e

Browse files
Merge pull request #3664 from MicrosoftDocs/alvinashcraft/main-cambrian-files-and-folders
Create files and folders content for Windows App SDK (Project Cambrian)
2 parents daf668d + 1cae047 commit 2cb0c8e

11 files changed

+343
-15
lines changed

hub/apps/develop/data-and-files.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ The [Windows App SDK](../windows-app-sdk/index.md) provides the following featur
2222

2323
Windows 10 and later OS releases provide a wide variety of APIs related to data and files scenarios for apps. These features are available via a combination of WinRT and Win32 (C++ and COM) APIs provided by the [Windows SDK](https://developer.microsoft.com/windows/downloads/windows-sdk).
2424

25-
#### WinRT APIs
25+
### WinRT APIs
2626

2727
The following articles provide information about features available via WinRT APIs provided by the Windows SDK.
2828

@@ -32,7 +32,7 @@ The following articles provide information about features available via WinRT AP
3232
| [Data binding](/windows/uwp/data-binding/) | Learn how to bind your app's UI to data and keep the UI in sync with changes to the data. |
3333
| [Files, folders, and libraries](/windows/uwp/files/) | Learn how to read and write text and other data formats in files, and to manage files and folders. |
3434

35-
#### Win32 (C++ and COM) APIs
35+
### Win32 (C++ and COM) APIs
3636

3737
The following articles provide information about features available via Win32 (C++ and COM) APIs provided by the Windows SDK.
3838

hub/apps/develop/data-binding/bind-to-hierarchical-data-and-create-a-master-details-view.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
22
ms.assetid: 6c563dd4-3dd0-4175-a1ab-7a1103fc9559
3-
title: Bind hierarchical data and create a master/details view
3+
title: Bind hierarchical data and create a master/details view with Windows App SDK
44
description: You can make a multi-level master/details (also known as list-details) view of hierarchical data by binding items controls to CollectionViewSource instances that are bound together in a chain.
55
ms.date: 12/13/2022
66
ms.topic: article
77
keywords: windows 10, windows 11, winui, windows app sdk, windows ui, xBind
88
ms.localizationpriority: medium
99
---
1010

11-
# Bind hierarchical data and create a master/details view
11+
# Bind hierarchical data and create a master/details view with Windows App SDK
1212

1313
> [!NOTE]
1414
> Also see the [Master/detail UWP sample](https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/XamlMasterDetail).

hub/apps/develop/data-binding/data-binding-and-mvvm.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
22
ms.assetid: b7a8ec88-3013-4e5a-a110-fab3f20ee4bf
3-
title: Data binding and MVVM
3+
title: Windows data binding and MVVM
44
description: Data binding is at the core of the Model-View-ViewModel (MVVM) UI architectural design pattern, and enables loose coupling between UI and non-UI code.
55
ms.date: 12/13/2022
66
ms.topic: article
77
keywords: windows 10, windows 11, windows app sdk, winui, windows ui, mvvm
88
ms.localizationpriority: medium
99
---
1010

11-
# Data binding and MVVM
11+
# Windows data binding and MVVM
1212

1313
Model-View-ViewModel (MVVM) is a UI architectural design pattern for decoupling UI and non-UI code. With MVVM, you define your UI declaratively in XAML and use data binding markup to link it to other layers containing data and commands. The data binding infrastructure provides a loose coupling that keeps the UI and the linked data synchronized and routes user input to the appropriate commands.
1414

hub/apps/develop/data-binding/data-binding-overview.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
ms.assetid: 02a08657-285d-4804-a006-168c22aa4904
3-
title: Data binding overview
3+
title: Windows data binding overview
44
description: This topic shows you how to bind a control (or other UI element) to a single item or bind an item's control to a collection of items in a Windows App SDK app.
55
ms.date: 12/12/2022
66
ms.topic: article
@@ -10,7 +10,7 @@ dev_langs:
1010
- csharp
1111
---
1212

13-
# Data binding overview
13+
# Windows data binding overview
1414

1515
This topic shows you how to bind a control (or other UI element) to a single item or bind an items control to a collection of items in a Windows App SDK app. In addition, we show how to control the rendering of items, implement a details view based on a selection, and convert data for display. For more detailed info, see [Data binding in depth](data-binding-in-depth.md).
1616

hub/apps/develop/data-binding/function-bindings.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
description: Learn about using functions as the leaf step of the data binding path in the xBind markup extension.
3-
title: Functions in x:Bind
3+
title: Functions in x:Bind with Windows App SDK
44
ms.date: 12/13/2022
55
ms.topic: article
66
keywords: windows 10, windows 11, windows app sdk, xaml, winui, windows ui, xBind
77
ms.localizationpriority: medium
88
---
99

10-
# Functions in x:Bind
10+
# Functions in x:Bind with Windows App SDK
1111

1212
> [!NOTE]
1313
> For general info about using data binding in your app with `{x:Bind}` (and for an all-up comparison between `{x:Bind}` and `{Binding}`), see [Data binding in depth](data-binding-in-depth.md) and [{x:Bind} Markup Extension](/windows/uwp/xaml-platform/x-bind-markup-extension).
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
---
2+
ms.assetid: 5931d63c-6b80-4e47-b371-ee299e308b8e
3+
title: Access files and folders with Windows App SDK and .NET
4+
description: Packaged Windows App SDK apps can leverage .NET APIs for reading and writing files, working with folders, and reading drive and volume information.
5+
ms.date: 06/16/2023
6+
ms.topic: article
7+
keywords: windows 10, windows 11, windows, winui, windows app sdk, dotnet
8+
ms.localizationpriority: medium
9+
---
10+
# Access files and folders with Windows App SDK and .NET
11+
12+
Packaged Windows App SDK apps can leverage [.NET APIs](/dotnet/) for reading and writing files, working with folders, and reading drive and volume information. 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 .NET [System.IO](/dotnet/api/system.io) APIs to read and write files, manage drives and folders, and work with memory streams to encode or decode string data.
13+
14+
## Read and write files with .NET APIs
15+
16+
In the following example, `ReadWriteFiles` creates a new file, writes a set of integers to the file, and then reads the integers back from the file. The example uses the [FileStream](/dotnet/api/system.io.filestream) class to create a new file and to open the file for reading or writing. The example uses the [BinaryWriter](/dotnet/api/system.io.binarywriter) class to write the integers to the file and the [BinaryReader](/dotnet/api/system.io.binaryreader) class to read the integers from the file.
17+
18+
```csharp
19+
using System.IO;
20+
...
21+
ReadWriteFiles("test.bin");
22+
...
23+
private void ReadWriteFiles(string fileName)
24+
{
25+
if (File.Exists(fileName))
26+
{
27+
Console.WriteLine($"{fileName} already exists!");
28+
return;
29+
}
30+
31+
using (FileStream fs = new(fileName, FileMode.CreateNew))
32+
{
33+
using BinaryWriter writer = new(fs);
34+
for (int i = 0; i < 11; i++)
35+
{
36+
writer.Write(i);
37+
}
38+
}
39+
40+
using (FileStream fs = new(fileName, FileMode.Open, FileAccess.Read))
41+
{
42+
using BinaryReader reader = new(fs);
43+
for (int i = 0; i < 11; i++)
44+
{
45+
Console.WriteLine(reader.ReadInt32());
46+
}
47+
}
48+
}
49+
```
50+
51+
## Manage drives and folders in .NET
52+
53+
The following example shows how to use the [DirectoryInfo](/dotnet/api/system.io.directoryinfo) and [Directory](/dotnet/api/system.io.directory) classes to create, delete, and manage folders. The example uses the `DirectoryInfo` class to create a new directory, create a subdirectory, and delete the directory. The `DirectoryInfo` class provides methods for creating, moving, and enumerating through directories and subdirectories. The `Directory` class provides *static* methods for creating, moving, and enumerating through directories and subdirectories.
54+
55+
```csharp
56+
using System.IO;
57+
...
58+
private void FolderTest()
59+
{
60+
FolderManagement(@"c:\MyDir", "Projects");
61+
}
62+
private void FolderManagement(string path, string subfolderName)
63+
{
64+
DirectoryInfo di = new(path);
65+
try
66+
{
67+
// Create directory if it doesn't exist
68+
if (di.Exists)
69+
{
70+
Console.WriteLine("Path already exists.");
71+
}
72+
else
73+
{
74+
di.Create();
75+
Console.WriteLine("The directory was created successfully.");
76+
}
77+
78+
// Create subdirectory if it doesn't exist
79+
string subfolderPath = Path.Combine(path, subfolderName);
80+
if (Directory.Exists(subfolderPath))
81+
{
82+
Console.WriteLine("Subfolder path already exists.");
83+
}
84+
else
85+
{
86+
di.CreateSubdirectory(subfolderName);
87+
Console.WriteLine("The subdirectory was created successfully.");
88+
}
89+
90+
// Delete directory
91+
di.Delete(true);
92+
Console.WriteLine("The directory was deleted successfully.");
93+
}
94+
catch (Exception ex)
95+
{
96+
Console.WriteLine("The process failed: {0}", ex.ToString());
97+
}
98+
}
99+
```
100+
101+
This example using the static [GetDrives](/dotnet/api/system.io.driveinfo.getdrives) method to retrieve information about all drives on the system. The [DriveInfo](/dotnet/api/system.io.driveinfo) class provides information about a drive, such as the drive type, label, file system, and available free space.
102+
103+
```csharp
104+
using System.IO;
105+
...
106+
private void DriveManagement()
107+
{
108+
DriveInfo[] drives = DriveInfo.GetDrives();
109+
110+
foreach (DriveInfo d in drives)
111+
{
112+
Console.WriteLine($"Drive name: {d.Name}");
113+
Console.WriteLine($" Drive type: {d.DriveType}");
114+
if (d.IsReady)
115+
{
116+
Console.WriteLine($" Volume label: {d.VolumeLabel}");
117+
Console.WriteLine($" File system type: {d.DriveFormat}");
118+
Console.WriteLine($" Space available to user: {d.AvailableFreeSpace, 15} bytes");
119+
Console.WriteLine($" Total available space: {d.TotalFreeSpace, 15} bytes");
120+
Console.WriteLine($" Total size of drive: {d.TotalSize, 15} bytes ");
121+
}
122+
}
123+
}
124+
```
125+
126+
## Encode and decode strings with MemoryStream
127+
128+
This example shows how to use the [MemoryStream](/dotnet/api/system.io.memorystream) class to encode and decode string data. It first creates a `MemoryStream` to asynchronously write a string to a memory stream and then read the string from the memory stream. The [Encoding](/dotnet/api/system.text.encoding) class is used to convert the string to a byte array and then write the byte array to the memory stream. A [StreamReader](/dotnet/api/system.io.streamreader) is then used to asynchronously read the byte array from the memory stream and then convert the byte array back to a string by calling [ReadToEndAsync](/dotnet/api/system.io.streamreader.readtoendasync).
129+
130+
```csharp
131+
using System.IO;
132+
using System.Text;
133+
...
134+
private async Task EncodeDecodeStringAsync(string inputData)
135+
{
136+
using MemoryStream stream = new();
137+
var inputBytes = Encoding.UTF8.GetBytes(inputData);
138+
await stream.WriteAsync(inputBytes, 0, inputBytes.Length);
139+
stream.Seek(0, SeekOrigin.Begin);
140+
141+
using StreamReader reader = new(stream);
142+
string text = await reader.ReadToEndAsync();
143+
Console.WriteLine(text);
144+
}
145+
```
146+
147+
> [!NOTE]
148+
> For informatio about converting between .NET streams and WinRT streams, see [How to: Convert between .NET and Windows Runtime streams](/dotnet/standard/io/how-to-convert-between-dotnet-streams-and-winrt-streams).
149+
150+
## See also
151+
152+
[Access files and folders with Windows App SDK and WinRT APIs](winrt-files.md)
153+
154+
[Files, folders, and libraries with Windows App SDK](index.md)

hub/apps/develop/files/index.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
ms.assetid: 33d8df1e-898d-48b7-9399-1aee673490d9
3+
title: Files, folders, and libraries with Windows App SDK
4+
description: Learn about reading and writing app settings, file and folder pickers, and special sand-boxed locations such as the Video/Music library with Windows App SDK apps.
5+
ms.date: 06/16/2023
6+
ms.topic: article
7+
keywords: windows 10, windows 11, windows, winui, windows app sdk, winrt, dotnet
8+
ms.localizationpriority: medium
9+
---
10+
# Files, folders, and libraries with Windows App SDK
11+
12+
Packaged Windows App SDK apps can leverage the powerful APIs provided by the [Windows.Storage](/uwp/api/Windows.Storage), [Windows.Storage.Streams](/uwp/api/Windows.Storage.Streams), and [Windows.Storage.Pickers](/uwp/api/Windows.Storage.Pickers) namespaces to efficiently read and write various data formats in files, as well as manage files and folders. This section covers essential topics such as reading and writing app settings, file and folder pickers, and accessing special sand-boxed locations like the Video/Music library. Learn how to optimize your app's file management capabilities with the Windows App SDK.
13+
14+
Windows 10 and later OS releases provide a wide variety of APIs related to files, folders, libraries, and settings for apps. These features are available via a combination of WinRT and .NET APIs provided by the [Windows SDK](https://developer.microsoft.com/windows/downloads/windows-sdk) and [.NET SDK](https://dotnet.microsoft.com/en-us/download).
15+
16+
## Read and write data with WinRT storage APIs
17+
18+
Packaged apps have access to all the WinRT storage APIs available to UWP apps. Whether you're migrating an existing UWP app or creating a new app, you can use these APIs to read and write data. For examples of using the storage APIs in a WinUI app, see [Access files and folders with Windows App SDK and WinRT APIs](winrt-files.md).
19+
20+
## Read and write data with .NET file APIs
21+
22+
In addition to the WinRT APIs, packaged apps can use the .NET APIs in the [System.IO](/dotnet/api/system.io) namespace to read and write data. When a new WinUI 3 project is created, its `Package.appxmanifest` file contains the following setting:
23+
24+
``` xml
25+
<Capabilities>
26+
<rescap:Capability Name="runFullTrust" />
27+
</Capabilities>
28+
```
29+
30+
Declaring this restricted capability provides full access to the file system, registry, and other restricted capabilities. For more info, see [Restricted capability declarations](/windows/uwp/packaging/app-capability-declarations#restricted-capability-declarations). In other words, the app has the same access to the file system as any other .NET desktop app when using the .NET file APIs.
31+
32+
For examples of using the .NET APIs in a WinUI app, see [Access files and folders with Windows App SDK and .NET APIs](dotnet-files.md).
33+
34+
## Additional resources for working with files and folders
35+
36+
If you're developing packaged WinUI apps, the WinRT storage APIs can be a powerful tool for reading and writing data. The following UWP topics provide a wealth of information for developers looking to leverage these APIs in their apps.
37+
38+
| Topic | Description |
39+
|-------|-------------|
40+
| [Enumerate and query files and folders](/windows/uwp/files/quickstart-listing-files-and-folders) | Access files and folders in either a folder, library, device, or network location. You can also query the files and folders in a location by constructing file and folder queries. |
41+
| [Create, write, and read a file](/windows/uwp/files/quickstart-reading-and-writing-files) | Read and write a file using a [StorageFile](/uwp/api/Windows.Storage.StorageFile) object. |
42+
| [Best practices for writing to files](/windows/uwp/files/best-practices-for-writing-to-files) | Learn best practices for using various file writing methods of the [FileIO](/uwp/api/windows.storage.fileio) and [PathIO](/uwp/api/windows.storage.pathio) classes. |
43+
| [Get file properties](/windows/uwp/files/quickstart-getting-file-properties) | Get properties—top-level, basic, and extended—for a file represented by a [StorageFile](/uwp/api/Windows.Storage.StorageFile) object. |
44+
| [Open files and folders with a picker](/windows/uwp/files/quickstart-using-file-and-folder-pickers) | Access files and folders by letting the user interact with a picker. You can use the [FolderPicker](/uwp/api/Windows.Storage.Pickers.FolderPicker) to gain access to a folder. |
45+
| [Save a file with a picker](/windows/uwp/files/quickstart-save-a-file-with-a-picker) | Use [FileSavePicker](/uwp/api/Windows.Storage.Pickers.FileSavePicker) to let users specify the name and location where they want your app to save a file. |
46+
| [Accessing HomeGroup content](/windows/uwp/files/quickstart-accessing-homegroup-content) | Access content stored in the user's HomeGroup folder, including pictures, music, and videos. |
47+
| [Determining availability of Microsoft OneDrive files](/windows/uwp/files/quickstart-determining-availability-of-microsoft-onedrive-files) | Determine if a Microsoft OneDrive file is available using the [StorageFile.IsAvailable](/uwp/api/windows.storage.storagefile.isavailable) property. |
48+
| [Files and folders in the Music, Pictures, and Videos libraries](/windows/uwp/files/quickstart-managing-folders-in-the-music-pictures-and-videos-libraries) | Add existing folders of music, pictures, or videos to the corresponding libraries. You can also remove folders from libraries, get the list of folders in a library, and discover stored photos, music, and videos. |
49+
| [Track recently used files and folders](/windows/uwp/files/how-to-track-recently-used-files-and-folders) | Track files that your user accesses frequently by adding them to your app's most recently used list (MRU). The platform manages the MRU for you by sorting items based on when they were last accessed, and by removing the oldest item when the list's 25-item limit is reached. All apps have their own MRU. |
50+
| [Track file system changes in the background](/windows/uwp/files/change-tracking-filesystem) | Track changes to the file system, even when the app isn't running.|
51+
| [Access the SD card](/windows/uwp/files/access-the-sd-card) | You can store and access non-essential data on an optional microSD card, especially on low-cost mobile devices that have limited internal storage. |
52+
| [Fast access to file properties](/windows/uwp/files/fast-file-properties) | Efficiently gather a list of files and their properties from a library to use via the Windows Runtime APIs. |
53+
54+
## See also
55+
56+
[Access files and folders with Windows App SDK and WinRT APIs](winrt-files.md)
57+
58+
[Access files and folders with Windows App SDK and .NET APIs](dotnet-files.md)
59+
60+
[System.IO](/dotnet/api/system.io)

0 commit comments

Comments
 (0)