2017-02-24 2 views
2

XAML 1 단추와 이름이 UserInputTextBox 및 StatusTextBox 인 2 개의 TEXTBOXES에서 생성했습니다. 그럼 난 파일을 열고 파일에 텍스트를 저장하는 MainPage.xaml.cs를 코드에서 만든 :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)로 될 것입니다하지만 내 문제는 내가 만약 UserInputTextBox 텍스트에 두 번째로 쓰면 첫 번째 텍스트가 두 번째 텍스트로 변경됩니다. 내가 원하는 것은 UserInputTextBox에 텍스트를 써서이 텍스트를 저장해야하고 두 번째 텍스트를 쓰면 두 개의 텍스트가있는 것입니다.

답변

2

어떤 (텍스트) 파일이 무엇이며 어떻게 작동하는지 생각해야합니다. 어쨌든, 당신은 그것을 덮어 쓰기보다는 텍스트를 추가하려고합니다.

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

시도해보십시오. 그러면 결과가 어쨌든 구분되지 않습니다. 이를 위해 NewLine 캐릭터를 연구하거나 AppendLinesAsync()을 볼 수 있습니다.

관련 문제