Reputation: 11
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. 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.
Upvotes: 1
Views: 3054
Reputation: 1301
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. 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.
Hope this helps..
Upvotes: 1