2017-02-24 23 views
2

XAML 1ボタンと2つのTEXTBOXESでUserInputTextBoxとStatusTextBoxという名前で作成しました。UWPファイルにテキストを保存

FileOpenPicker picker = new FileOpenPicker();  
private async void Button_Click(object sender, RoutedEventArgs e) 
{ 

    // Set properties on the file picker such as start location and the type 
    // of files to display. 
    picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; 
    picker.ViewMode = PickerViewMode.List; 
    picker.FileTypeFilter.Add(".txt"); 

    // Show picker enabling user to pick one file. 
    StorageFile result = await picker.PickSingleFileAsync(); 

    if (result != null) 
    { 
     try 
     { 
      // Use FileIO to replace the content of the text file 
      await FileIO.WriteTextAsync(result, UserInputTextBox.Text); 

      // Display a success message 
      StatusTextBox.Text = "Status: File saved successfully"; 
     } 
     catch (Exception ex) 
     { 
      // Display an error message 
      StatusTextBox.Text = "Status: error saving the file - " + ex.Message; 
     } 
    } 
    else 
     StatusTextBox.Text = "Status: User cancelled save operation"; 
} 

と私はUserInputTextBoxテキストに書き込む場合は、このテキストファイル(.TXT)になりますが、私ならば、私の問題がある:私は、ファイルを開くと、ファイルにテキストを保存するためにMainPage.xaml.csコードで作成されましたUserInputTextBoxテキストに2回目に書き込み、最初のテキストは2番目のテキストに変更されます。私は何をしたいのですか?私はUserInputTextBoxにテキストを書き込んで、このテキストを保存しなければならない、そして2回目にテキストを書く場合は2つのテキストがあります。

答えて

2

何(テキスト)ファイルがどのようなもので、どのように機能するか考えるべきです。いずれにしても、上書きするのではなく、テキストを追加することを検討しています。

//await FileIO.WriteTextAsync(result, UserInputTextBox.Text); 
    await FileIO.AppendTextAsync(result, UserInputTextBox.Text); 

これを試してください。とにかく結果が分かれていないことがわかります。そのためには、NewLineの文字を調べたり、AppendLinesAsync()を見ることができます。

関連する問題