简体   繁体   English

UWP 应用尝试将文件保存到文件夹

[英]UWP app trying to save a file to a folder

I am currently developing a UWP app and I want to be able to open and read a .txt file from the virtual sd card, which I have working.我目前正在开发一个 UWP 应用程序,我希望能够从我正在使用的虚拟 SD 卡中打开和读取 .txt 文件。 What I am stuck on is when I view that file I want to add a button to the botton of the page that allows me to save a copy of that file to a specific location on the local machine.我遇到的问题是,当我查看该文件时,我想在页面的底部添加一个按钮,该按钮允许我将该文件的副本保存到本地计算机上的特定位置。 Below is the code for the current opening and reading of the file.下面是当前打开和读取文件的代码。

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();

        }
     }
}

Any help would greatfully appreciated.任何帮助将不胜感激。

You can use the following code to save a text file to local folder .您可以使用以下代码将文本文件保存到本地文件夹。

Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile sampleFile = await storageFolder.CreateFileAsync("sample.txt",
Windows.Storage.CreationCollisionOption.ReplaceExisting);

Here is a MSDN article on saving a file with the file picker for UWP applications.这是一篇关于使用 UWP 应用程序的文件选择器保存文件的 MSDN 文章。 Link Here 链接在这里

It fully explains how to customize the file save picker and update the UI to show the user that the file has been saved successfully or any errors during the operation.它充分说明了如何自定义文件保存选择器并更新 UI 以向用户显示文件已成功保存或操作过程中出现任何错误。

Hope this helps..希望这可以帮助..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM