-1

This question has been answered. I've improved the code a bit (at least I think so). It now reminds of the aceepted answer to the question Open file in rich text box with C#. If I haven't made any mistakes (which I may have), the code should save a file with text from the rich text box rtfMain. The default file extension is .txt. You can also use the file extension .rtf.

private void menuFileSave_Click(object sender, EventArgs e) 
{ 
// Create a new SaveFileDialog object 
using (SaveFileDialog dlgSave = new SaveFileDialog())
  try
  {
    // Default file extension
    dlgSave.DefaultExt = "txt"; 
    // SaveFileDialog title
    dlgSave.Title = "Save File As";
    // Available file extensions
    dlgSave.Filter = "Text Files (*.txt)|*.txt|RTF Files (*.rtf)|*.rtf"; 
    // Show SaveFileDialog box and save file
    if (dlgSave.ShowDialog() == DialogResult.OK) 
    { 
      // Save as .txt file
      if (Path.GetExtension(dlgSave.FileName) == ".txt")
      {
        rtfMain.SaveFile(dlgSave.FileName, RichTextBoxStreamType.PlainText);
      }
      // Save as .rtf file
      if (Path.GetExtension(dlgSave.FileName) == ".rtf")
      {
        rtfMain.SaveFile(dlgSave.FileName, RichTextBoxStreamType.PlainText);
      }
    }
    catch (Exception errorMsg)
    {
      MessageBox.Show(errorMsg.Message);
    }
  }
}
private void rtfMain_TextChanged(object sender, EventArgs e)
{
}

Update: I have improved the code even further (at least I think so). The main difference is that you now have more control over the file encoding. This is the code I'm using right now:

private void fileSave_Click(object sender, EventArgs e)
{
  // Text from the rich textbox rtfMain
  string str = rtfMain.Text;
  // Create a new SaveFileDialog object
  using (SaveFileDialog dlgSave = new SaveFileDialog())
  try
  {
    // Available file extensions
    dlgSave.Filter = "All Files (*.*)|*.*";
    // SaveFileDialog title
    dlgSave.Title = "Save";
    // Show SaveFileDialog
    if (dlgSave.ShowDialog() == DialogResult.OK && dlgSave.FileName.Length > 0)
    {
      // Save file as utf8 without byte order mark (BOM)
      // ref: http://msdn.microsoft.com/en-us/library/s064f8w2.aspx
      UTF8Encoding utf8 = new UTF8Encoding();
      StreamWriter sw = new StreamWriter(dlgSave.FileName, false, utf8);
      sw.Write(str);
      sw.Close();
    }
  }
  catch (Exception errorMsg)
  {
    MessageBox.Show(errorMsg.Message);
  } 
}
Community
  • 1
  • 1
matsolof
  • 2,665
  • 3
  • 18
  • 17
  • 3
    You should use the `using` statement. – SLaks Sep 20 '10 at 13:33
  • According to http://msdn.microsoft.com/en-us/library/yh598w02(VS.80).aspx, "it is usually best to release limited resources such as file handles and network connections as quickly as possible" and "the using statement allows the programmer to specify when objects that use resources should release them". Is this the main reason for using the using statement? – matsolof Sep 22 '10 at 10:47
  • Yup, this is old, but your code helped me –  Oct 27 '12 at 05:50

2 Answers2

4

Like this:

 rtfMain.SaveFile(dlgSave.FileName);
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    SLaks way is the easiest. But to get your own code running, you could add writeMe.Flush() after writing to it. – Hinek Sep 20 '10 at 13:44
  • Thank you! Where do I put your code? I tried every position I could think of, but nothing worked. – matsolof Sep 20 '10 at 13:54
  • No. I'm a C# beginner. I'm just trying to make some basic code work and continue from there. Having a couple of years experience of PHP, that seems to remind a lot of C#, I think that's a good idea. Earlier today, I managed to get an open file dialog to work. Now I'm trying to do the same with a save file dialog. – matsolof Sep 20 '10 at 14:33
  • 2
    Put it after `if (dlgSave.ShowDialog() == DialogResult.OK)` and remove the rest of the method, then learn C#. – SLaks Sep 20 '10 at 14:54
  • Then you should accept this answer by clicking the hollow check. – SLaks Sep 20 '10 at 16:51
  • I know. I forgot to do it. Sorry about that. It's fixed now. – matsolof Sep 21 '10 at 06:33
-3

Your code here saves .doc files formatted. When I use it to save .docx files it does save it but when I try to open the saved file using Microsoft Word, An error message is displayed.