如何通过 saveFiledialog 将 Richboxtext 内容保存到 .txt 文件? [英] How to save richboxtext content to .txt file via saveFiledialog?

查看:36
本文介绍了如何通过 saveFiledialog 将 Richboxtext 内容保存到 .txt 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为自己创建一个简单的记事本.目前我已经创建了一个按钮,我可以在其中打开文件并获取内容.如何创建保存文件按钮,以便 Richboxtext 的每一行都转到新的 .txt 文件?

I'm trying to create a simple notepad for my self. At the moment i have created a button where i can Open file and get content. How can i create a Save file button so every line of my richboxtext goes to new .txt file?

我希望我可以保存到我选择的文件名而不是静态文件名,所以我使用 SaveFileDialog.

I want that i can save to a file name of my choice not a static one so i use SaveFileDialog.

这是我打开文件的内容.

This is what i have for Open File.

if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
    Stream plusma = openFileDialog1.OpenFile();

    string faila_nosaukums = openFileDialog1.FileName;
    string faila_teksts = File.ReadAllText(faila_nosaukums);
    rtf_NotePad.Text = faila_teksts;
}

目前没有保存文件:

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{

}

----------更新-----------------
保存文件:

            saveFileDialog1.Filter = "Text Files (*.txt)|*.txt|RTF Files (*.rtf)|*.rtf";
            saveFileDialog1.AddExtension = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                rtf_NotePad.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
            }

添加颜色:

            colorDialog1.ShowDialog();
            rtf_NotePad.ForeColor = colorDialog1.Color;

推荐答案

您可以使用 RichTextBox.SaveFile() 方法:

You can use the RichTextBox.SaveFile() method:

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
    rtf_NotePad.SaveFile(saveFileDialog1.FileName);
}

此外,还有一种类似的打开文件的方法.它是 RichTextBox.LoadFile() 方法.像这样使用它:

Also, there exists a similar method for opening the file. It is the RichTextBox.LoadFile() method. Use it like this:

if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
    rtf_NotePad.LoadFile(openFileDialog1.FileName);
}

这篇关于如何通过 saveFiledialog 将 Richboxtext 内容保存到 .txt 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆