简体   繁体   English

如何在RTF文本框中保存打开的文件

[英]How to save a opened file in richtext box

txt file in my richtextbox and want to save the orginal file rather than save a new file (SaveAs). 我的richtextbox中的txt文件,并且想要保存原始文件而不是保存新文件(SaveAs)。 here is my code for saving the file 这是我保存文件的代码

private void SaveMyFile_Click(object sender, EventArgs e)
    {

        SaveFileDialog saveFile1 = new SaveFileDialog();
        saveFile1.DefaultExt = "*.rtf";
        saveFile1.Filter = "RTF Files|*.rtf";          
        if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
        saveFile1.FileName.Length > 0)
        {
          richTextBox1.SaveFile(saveFile1.FileName, RichTextBoxStreamType.PlainText);
        }

    }

any help pleaseeee 任何帮助

Are you saying that you should don't want to present the dialog? 您是说不想显示对话框吗? If so, just save the filename and save using that: 如果是这样,只需保存文件名并使用以下文件名进行保存:

richTextBox1.SaveFile(_filename, RichTextBoxStreamType.PlainText);

打开文件时,只需保存文件路径即可。然后丢失所有SaveFileDialog内容,然后调用richTextBox1.SaveFile(the_path_you_saved_when_you_opened_the_file);

Just save the original path to the file that was read into the textbox. 只需将原始路径保存到已读入文本框中的文件即可。 When the user clicks the button to save, use the following code: 当用户单击按钮进行保存时,使用以下代码:

richTextBox1.SaveFile(filename, RichTextBoxStreamType.PlainText);

Where filename is the variable you stored the original path to the file opened. 其中filename是用于存储打开的文件的原始路径的变量。

This is the code that it sounds like you want: 这是听起来像您想要的代码:

String fileLocation;
private void SaveMyFile_Click(object sender, EventArgs e)
{
    var performSave = true;
    if(String.IsNullOrEmpty(fileLocation))
    {
        performSave = SetFileLocation();
    }
    if(performSave)
        richTextBox1.SaveFile(fileLocation, RichTextBoxStreamType.PlainText);

}

private bool SetFileLocation()
{
        SaveFileDialog saveFile1 = new SaveFileDialog();
        saveFile1.DefaultExt = "*.rtf";
        saveFile1.Filter = "RTF Files|*.rtf";          
        if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
        saveFile1.FileName.Length > 0)
        {
          fileLocation = saveFile1.FileName;
          return true;
        }
        return false;
}

You should try adding the file name or FileInfo from which the rich text is loaded from. 您应该尝试添加从中加载富文本格式的文件名或FileInfo。 If the file has not been saved, prompt to save the file. 如果尚未保存文件,则提示保存文件。 Otherwise, save to the file info cached. 否则,将缓存的信息保存到文件中。

partial class YourForm : Form
{
    string filePath;

    private void SaveMyFile_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(filePath))
        {
            SaveFileDialog saveFile1 = new SaveFileDialog();
            saveFile1.DefaultExt = "*.rtf";
            saveFile1.Filter = "RTF Files|*.rtf";          
            if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
                saveFile1.FileName.Length > 0)
            {
                filePath = saveFile1.FileName;
            }
            else return;
        }

        try
        {
            richTextBox1.SaveFile(filePath, RichTextBoxStreamType.PlainText);
        }
        catch (Exception ee)
        {
            // Put exception handling code here
        }
    }
}

As per comment below, if you'd like a save as button, you can try the following: 根据下面的评论,如果您想要另存为按钮,则可以尝试以下操作:

partial class YourForm : Form
{
    Button saveFileAsButton; // Add this using the Forms Designer

    private void saveFileAsButton_Click(object sender, EventArgs e)
    {
        SaveFileDialog saveFile1 = new SaveFileDialog();
        saveFile1.DefaultExt = "*.rtf";
        saveFile1.Filter = "RTF Files|*.rtf";          
        if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
            saveFile1.FileName.Length > 0)
        {
            try
            {
                richTextBox1.SaveFile(saveFile1.FileName, RichTextBoxStreamType.PlainText);
                filePath = saveFile1.FileName;
            }
            catch (Exception ee)
            {
                // Put exception handling code here (e.g. error saying file cannot be saved)
            }
        }
    }
}

Note how the setting of filePath is in the try block. 注意在try块中如何设置filePath In case the save failed, you don't want to lose your original file path. 万一保存失败,您不想丢失原始文件路径。

If you form has a MenuStrip , I recommend moving the save functions into the menus. 如果您的表单具有MenuStrip ,则建议将保存功能移至菜单中。

(BTW, the type name for RTF in your filter would be better as " Rich Text Document ".) (顺便说一句,过滤器中RTF的类型名称最好是“ Rich Text Document ”。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM