Ejemplo n.º 1
0
        //Import file from speciafied folder
        private async void AppBarButton_ImportFile(object sender, RoutedEventArgs e)
        {
            Windows.Storage.Pickers.FileOpenPicker open =
                new Windows.Storage.Pickers.FileOpenPicker();
            open.SuggestedStartLocation =
                Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
            //Show only txt files
            open.FileTypeFilter.Add(".txt");

            Windows.Storage.StorageFile file = await open.PickSingleFileAsync();

            //If file is opened succesfully
            if (file != null)
            {
                //Try catch block for readng file streams
                try
                {
                    //Open stream for reading
                    Windows.Storage.Streams.IRandomAccessStream randAccStream =
                        await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

                    //Create StreamReader object with random access stream object as argument
                    StreamReader stream = new StreamReader(randAccStream.AsStream());
                    //Read stream to end and store in String type
                    String text = stream.ReadToEnd();
                    //Load string to editor box
                    editor.Text = text;
                }
                //Catch any exceptions if try block unsuccesful
                catch (Exception)
                {
                    //Create and display error dialog
                    ContentDialog errorDialog = new ContentDialog()
                    {
                        Title             = "File open error",
                        Content           = "Sorry, I couldn't open the file.",
                        PrimaryButtonText = "Ok"
                    };

                    await errorDialog.ShowAsync();
                }
            }
        }
Ejemplo n.º 2
0
 private void OnCoreCloseFileStream(Windows.Storage.Streams.IRandomAccessStream stream)
 {
     StreamProvider.CloseStream(stream.AsStream());
 }