UWP应用程序试图将文件保存到文件夹
问题描述:
我目前正在开发一个UWP应用程序,我希望能够从虚拟SD卡上打开和读取.txt文件,我正在使用该文件。我遇到的问题是,当我查看该文件时,我想将一个按钮添加到页面底部,允许我将该文件的副本保存到本地计算机上的特定位置。以下是当前打开和阅读文件的代码。UWP应用程序试图将文件保存到文件夹
private async void Grid_Loaded(object sender, RoutedEventArgs e)
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
openPicker.FileTypeFilter.Add(".txt");
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
using(StreamReader reader = new StreamReader(stream.AsStream()))
{
txtBLKallCloudReceipts.Text = reader.ReadToEnd();
}
}
}
任何帮助将非常感激。
答
您可以使用下面的代码将文本文件保存到本地文件夹。
Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile sampleFile = await storageFolder.CreateFileAsync("sample.txt",
Windows.Storage.CreationCollisionOption.ReplaceExisting);
以下是关于使用UWP应用程序的文件选择器保存文件的MSDN文章。 Link Here
它完全解释了如何自定义文件保存选取器并更新UI以向用户显示该文件已成功保存或在操作过程中出现任何错误。
希望这会有所帮助..
好吧,很好,非常感谢您的帮助。 Lou – Bobbydazzler
请投票或回答标记这个问题。 – Pratyay