locked
Saving text within a textbox RRS feed

  • Question

  • Hello,

    I am trying to save text entered into a textbox using FileSavePicker. Below is the code of the button. I just don't know where or what to add in to save the content of the textbox.

            private async void SaveClose_Click(object sender, RoutedEventArgs e)
            {
    
                if (Notepad.Visibility == Visibility.Visible)
                {
    
                    FileSavePicker savePicker = new FileSavePicker();
                    savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
                    // Dropdown of file types the user can save the file as
                    savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
                    // Default file name if the user does not type one in or select a file to replace
                    savePicker.SuggestedFileName = "New Document";
                    StorageFile file = await savePicker.PickSaveFileAsync();
                    if (file != null)
                    {
                        // Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
                        CachedFileManager.DeferUpdates(file);
                        // write to file
                        await FileIO.WriteTextAsync(file, file.Name);
                        // Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
                        // Completing updates may require Windows to ask for user input.
                        FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
                        if (status == FileUpdateStatus.Complete)
                        {
                            OutputTextBlock.Text = "File " + file.Name + " was saved.";
                        }
                        else
                        {
                            OutputTextBlock.Text = "File " + file.Name + " couldn't be saved.";
                        }
                    }
                    else
                    {
                        OutputTextBlock.Text = "Operation cancelled.";
                    }
    
                    Notepad.Visibility = Visibility.Collapsed;
    
                }
            }

    Thanks for the help.

    John L


    John Lisenby developer of My PC Playbook

    Friday, April 19, 2013 2:06 PM

Answers

  • Assuming Textbox with name txtBoxContents.

    You can update this line

    await FileIO.WriteTextAsync(file, file.Name);

    as 

    await FileIO.WriteTextAsync(file, txtBoxContents.Text/*Text read from Textbox*/);

    See if this works?


    Thanks,
    Sachin
    My Samples | Personal Website


    • Edited by Sachin S Friday, April 19, 2013 2:51 PM
    • Marked as answer by John Lisenby Jr Friday, April 19, 2013 3:12 PM
    Friday, April 19, 2013 2:42 PM

All replies

  • Assuming Textbox with name txtBoxContents.

    You can update this line

    await FileIO.WriteTextAsync(file, file.Name);

    as 

    await FileIO.WriteTextAsync(file, txtBoxContents.Text/*Text read from Textbox*/);

    See if this works?


    Thanks,
    Sachin
    My Samples | Personal Website


    • Edited by Sachin S Friday, April 19, 2013 2:51 PM
    • Marked as answer by John Lisenby Jr Friday, April 19, 2013 3:12 PM
    Friday, April 19, 2013 2:42 PM
  • Thanks Sachin03,

    Adding that code worked!!!

    Thanks again,

    John


    John Lisenby developer of My PC Playbook

    Friday, April 19, 2013 3:13 PM