private async void WriteToStreamButton_Click(object sender, RoutedEventArgs e) { StorageFile file = rootPage.sampleFile; if (file != null) { try { string userContent = InputTextBox.Text; using (StorageStreamTransaction transaction = await file.OpenTransactedWriteAsync()) { IBuffer buffer = MainPage.GetBufferFromString(userContent); await transaction.Stream.WriteAsync(buffer); transaction.Stream.Size = buffer.Length; // truncate file await transaction.CommitAsync(); rootPage.NotifyUser($"The following text was written to '{file.Name}' using a stream:\n{userContent}", NotifyType.StatusMessage); } } catch (FileNotFoundException) { rootPage.NotifyUserFileNotExist(); } catch (Exception ex) { // I/O errors are reported as exceptions. rootPage.NotifyUser(String.Format("Error writing to '{0}': {1}", file.Name, ex.Message), NotifyType.ErrorMessage); } } else { rootPage.NotifyUserFileNotExist(); } }
private async void WriteBytesButton_Click(object sender, RoutedEventArgs e) { StorageFile file = rootPage.sampleFile; if (file != null) { try { string userContent = InputTextBox.Text; IBuffer buffer = MainPage.GetBufferFromString(userContent); await FileIO.WriteBufferAsync(file, buffer); rootPage.NotifyUser(String.Format("The following {0} bytes of text were written to '{1}':{2}{3}", buffer.Length, file.Name, Environment.NewLine, userContent), NotifyType.StatusMessage); } catch (FileNotFoundException) { rootPage.NotifyUserFileNotExist(); } catch (Exception ex) { // I/O errors are reported as exceptions. rootPage.NotifyUser(String.Format("Error writing to '{0}': {1}", file.Name, ex.Message), NotifyType.ErrorMessage); } } else { rootPage.NotifyUserFileNotExist(); } }