Search code examples
c#richtextboxstreamwriter

Error when opening a (silently) saved file c#


I have been making a text editor in c#, and recently added the functionality to silently save the file (without SaveFileDialog). The file appears to save properly, however, when trying to open the file, i get the error System.ArgumentException - File format is not valid. It opens fine if the file has not been saved silently.

The code:

The save method:

    public void save(RichTextBoxPrintCtrl.RichTextBoxPrintCtrl rtbIn, string fileNameIn)
    {
        string fileName = "";
        if (getFileFromMap(fileNameIn) != "")
        {
            // The file already exists in the Map so save it
            fileName = getFileFromMap(fileNameIn);
            StreamWriter writer = new StreamWriter(fileName);
            writer.Write(rtbIn.Text);
            writer.Close();
        }
        else
        {
            // The file does not exist in the Map so
            // Send it to SaveAs with the rtb and the initial fileName passed in
            saveAs(rtbIn, fileNameIn);
        }

    }

SaveAs:

    public string saveAs(RichTextBoxPrintCtrl.RichTextBoxPrintCtrl rtbIn, string fileName)
    {
        saveDialog.FileName = fileName;
        saveDialog.Title = "Save As";

        if (saveDialog.ShowDialog() == DialogResult.OK)
        {
            if (saveDialog.FileName.Length > 0)
            {
                if (saveDialog.FileName.EndsWith(".rtf"))
                {
                    rtbIn.SaveFile(saveDialog.FileName, RichTextBoxStreamType.RichText);
                }
                else
                {
                    rtbIn.SaveFile(saveDialog.FileName, RichTextBoxStreamType.PlainText);
                }
                addFileToMap(fileName, saveDialog.FileName);
                return Path.GetFileName(saveDialog.FileName);
            }
            else { return ""; }
        }
        else { return ""; }
    }

and Open:

    public string open(RichTextBoxPrintCtrl.RichTextBoxPrintCtrl rtbIn)
    {
        if (openDialog.ShowDialog() == DialogResult.OK)
        {
            if (openDialog.FileName.Length > 0)
            {
                string fileName = Path.GetFileName(openDialog.FileName);

                if (fileName.EndsWith(".rtf"))
                {
                    rtbIn.LoadFile(openDialog.FileName, RichTextBoxStreamType.RichText);
                }
                else
                {
                    rtbIn.LoadFile(openDialog.FileName, RichTextBoxStreamType.PlainText);
                }
                addFileToMap(openDialog.FileName, openDialog.FileName);
                return fileName;
            }
            else { return ""; }
        }
        else { return ""; }
    }

Other information:

  • The filenames are stored in a Dictionary because the editor has tabs.
  • RichTextBoxPrintCtrl is a custom RichTextBox that supports printing, it doesn't change anything relating to opening
  • The methods above are in a separate class which is why they require the richtextbox to be passed in.

If you need any other code, just let me know. Any advice would be appreciated! Thanks in advance :)

EDIT: Fixed, couldn't use StreamWriter.


Solution

  • Well, the issue seems to be that you are not saving the file in the same way.

    When you perform a saveAs, you are calling rtb.SaveFile. In your silent save you are directly trying to save the rtb.Text to the file but that is probably not the correct format rtb.OpenFile is expecting.

    I am no expert whatsoever in RichTextBox but spotting the difference when on method works and another similar one doesn't normally helps.

    To expand a little more, Text returns only the plain text (no content formatting information). Your method save is saving as plain text any file, even if its a .rtf. Your Open method on the other hand will try to open an .rtf file as a formatted text, this can be causing the issues you are having.