private async void ReadBytesButton_Click(object sender, RoutedEventArgs e) { StorageFile file = rootPage.sampleFile; if (file != null) { try { IBuffer buffer = await FileIO.ReadBufferAsync(file); string fileContent = MainPage.GetStringFromBuffer(buffer); rootPage.NotifyUser($"The following {buffer.Length} bytes of text were read from '{file.Name}':\n{fileContent}", NotifyType.StatusMessage); } catch (FileNotFoundException) { rootPage.NotifyUserFileNotExist(); } catch (Exception ex) when(ex.HResult == MainPage.E_NO_UNICODE_TRANSLATION) { rootPage.NotifyUser("File is not UTF-8 encoded.", NotifyType.ErrorMessage); } catch (Exception ex) { // I/O errors are reported as exceptions. rootPage.NotifyUser($"Error reading from '{file.Name}': {ex.Message}", NotifyType.ErrorMessage); } } else { rootPage.NotifyUserFileNotExist(); } }
private async void ReadFromStreamButton_Click(object sender, RoutedEventArgs e) { StorageFile file = rootPage.sampleFile; if (file != null) { try { using (IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read)) { UInt64 size64 = readStream.Size; if (size64 <= UInt32.MaxValue) { UInt32 size32 = (UInt32)size64; IBuffer buffer = new Windows.Storage.Streams.Buffer(size32); buffer = await readStream.ReadAsync(buffer, size32, InputStreamOptions.None); string fileContent = MainPage.GetStringFromBuffer(buffer); rootPage.NotifyUser($"The following text was read from '{file.Name}' using a stream:\n{fileContent}", NotifyType.StatusMessage); } else { rootPage.NotifyUser($"File {file.Name} is too big for ReadAsync to read in a single chunk. Files larger than 4GB need to be broken into multiple chunks to be read by ReadAsync.", NotifyType.ErrorMessage); } } } catch (FileNotFoundException) { rootPage.NotifyUserFileNotExist(); } catch (Exception ex) when(ex.HResult == MainPage.E_NO_UNICODE_TRANSLATION) { rootPage.NotifyUser("File is not UTF-8 encoded.", NotifyType.ErrorMessage); } catch (Exception ex) { // I/O errors are reported as exceptions. rootPage.NotifyUser($"Error reading from '{file.Name}': {ex.Message}", NotifyType.ErrorMessage); } } else { rootPage.NotifyUserFileNotExist(); } }