从LocalFolder保存文件

本文关键字:保存文件 LocalFolder | 更新日期: 2023-09-27 18:11:37

我在我的LocalFolder中有一个图像文件,当在应用程序中单击按钮时下载。

我需要使用FileSavePicker从LocalFolder移动图像到用户选择的另一个文件夹,例如Desktop.

图像在LocalFolder中,我用来保存它的代码是:

var imageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
         "image.png", CreationCollisionOption.ReplaceExisting);
var fs = await imageFile.OpenAsync(FileAccessMode.ReadWrite);
DataWriter writer = new DataWriter(fs.GetOutputStreamAt(0));
writer.WriteBytes(await response.Content.ReadAsByteArrayAsync());
await writer.StoreAsync();
writer.DetachStream();
await fs.FlushAsync();

当前我保存(不完整)的代码是:

FileSavePicker saver = new FileSavePicker();
saver.SuggestedStartLocation = PickerLocationId.Desktop;
saver.SuggestedFileName = "image.png";
StorageFile file = await saver.PickSaveFileAsync();

谁能告诉我如何才能做到这一点?

从LocalFolder保存文件

您应该利用FileIO来编写数据,因为它并不复杂。

var data = await response.Content.ReadAsByteArrayAsync();
var localFolder = ApplicationData.Current.LocalFolder;
var imageFile = await localFolder.CreateFileAsync(
         "image.png", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteBytesAsync(imageFile, data).AsTask();