c#UWP在没有对话框的情况下保存StorageFile
作者:互联网
关于FileSavePicker的一些问题,我遇到了麻烦.有没有关于保存StorageFile而没有显示任何弹出窗口或对话框来询问用户的解决方案.我想从后面的代码中提供存储文件的当前路径.
var byteArray = Convert.FromBase64String(Base64);
StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("file.jpg", CreationCollisionOption.ReplaceExisting);
await Windows.Storage.FileIO.WriteBytesAsync(file, byteArray);
var savePicker = new FileSavePicker();
savePicker.FileTypeChoices.Add("JPEG-Image", new List<string>() { ".jpg" });
savePicker.SuggestedSaveFile = file;
savePicker.PickSaveFileAsync();
解决方法:
…We have to use a few paths which are defined under
‘Windows.Storage.KnownFolders’…
不是这样.实际上,您的应用可以访问设备上的任何文件夹,但需要其他权限.获得许可的最直接的方法,你应该做下一步:
1)要求用户从FolderPicker中选择文件夹
2)将所选文件夹存储到StorageApplicationPermissions.FutureAccessList
在此之后,您的应用可以对此文件夹执行任何操作
演示如何获取权限的代码:
var picker = new FolderPicker();
var pfolder = await picker.PickSingleFolderAsync();
StorageApplicationPermissions.FutureAccessList.Add(pfolder);
演示如何在所需文件夹中创建文件的代码:
var folder = await StorageFolder.GetFolderFromPathAsync("your path");
var file = await folder.CreateFileAsync("text.txt");
using (var writer = await file.OpenStreamForWriteAsync())
{
await writer.WriteAsync(new byte[100], 0, 0);
}
但请记住,“您的路径”是存储到StorageApplicationPermissions.FutureAccessList的文件夹或任何子文件夹.更多细节在这里FutureAccessList
标签:c,win-universal-app,storagefile 来源: https://codeday.me/bug/20190528/1167445.html