- 1 year ago
How to save a file to downloads using WinUI 3?
Comment
1 / 1
To save a file to the Downloads folder using WinUI 3, you can use the KnownFolders
class to get a reference to the Downloads folder, and then use the FileIO
class to save the file.
Here's an example:
csharpusing Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
private async void SaveFileToDownloads()
{
// Get a reference to the Downloads folder
StorageFolder downloadsFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.Downloads);
// Create a new file in the Downloads folder
StorageFile file = await downloadsFolder.CreateFileAsync("example.txt", CreationCollisionOption.GenerateUniqueName);
// Write some text to the file
string text = "Hello, world!";
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
using (DataWriter writer = new DataWriter(stream))
{
writer.WriteString(text);
await writer.StoreAsync();
}
}
}
This code creates a new file named "example.txt" in the Downloads folder, and writes the string "Hello, world!" to the file.