使用 C# 保存富文本框中的文本

发布于 2024-09-19 19:57:46 字数 2242 浏览 3 评论 0原文

这个问题已经得到解答。我对代码做了一些改进(至少我这么认为)。现在它提醒了问题的可接受答案 在富文本框中打开文件使用 C#。如果我没有犯任何错误(我可能有),代码应该保存一个包含来自富文本框 rtfMain 的文本的文件。默认文件扩展名为 .txt。您还可以使用文件扩展名 .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)
{
}

更新:我进一步改进了代码(至少我这么认为)。主要区别在于您现在可以更好地控制文件编码。这是我现在使用的代码:

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);
  } 
}

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);
  } 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一笔一画续写前缘 2024-09-26 19:57:46

像这样:

 rtfMain.SaveFile(dlgSave.FileName);

Like this:

 rtfMain.SaveFile(dlgSave.FileName);
北城挽邺 2024-09-26 19:57:46

您此处的代码保存格式化的 .doc 文件。当我使用它保存 .docx 文件时,它确实会保存它,但当我尝试使用 Microsoft Word 打开保存的文件时,会显示错误消息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文