将富文本框内容直接保存到文件中。(不使用savefile对话框) [英] save rich textbox content directly into a file.(without using savefile dialog)

查看:393
本文介绍了将富文本框内容直接保存到文件中。(不使用savefile对话框)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何保存富文本框的内容而无需打开保存文件对话框。

i认为它类似于:



richTextBox1.SaveFile(@\Documents\save_file_here.rtf);



但它无法找到要覆盖的文件。

how can I save the contents of a rich text box without needing to open the save file dialog.
i think its something like:

richTextBox1.SaveFile(@"\Documents\save_file_here.rtf");

but it cant find the file to overwrite in the first place.

推荐答案

最简单的方法可能是:



The shortest way is probably:

File.WriteAllText(@"\Documents\save_file_here.rtf", richTextBox1.Text)





不确定是否会保存格式。



Not sure if that will save formatting though.


//create a file first
FileStream outStream = File.Create("Path \\filename");

//Create a variable
string data = richTextBox.Text;

//Save 
StreamWriter sw = new StreamWriter(outStream);
        sw.WriteLine(data);
        sw.Flush();
        sw.Close();


您在关注点分离时遇到问题。对话框与文件读/写操作无关。它们仅适用于文件系统,并允许确定要使用的文件名,而不是其他任何内容。对于读/写操作,它与文件名的来源完全无关。



此外,绝对不存在程序中名为硬编码的文件的情况有用,除非您从Visual Studio运行程序并始终修改它们。如果您尝试部署可执行文件,由于硬编码问题,它将会失效。文件名(偶数常量)总是在运行期间计算。



-SA
You have a problem with separation of concerns. Dialog boxes have nothing to do with file read/write operations. They only work with file system and allows to determine a file name to work with, nothing else. For read/write operations, it's absolutely irrelevant where the file name came from.

Also, there absolutely no situations where a file named hard-coded in the program could be useful, unless you run programs from the Visual Studio and modify them all the time. If you try to deploy executable, it will be defunct due to hard-coding problems. File names (even constant) are always calculated during run-time.

—SA


这篇关于将富文本框内容直接保存到文件中。(不使用savefile对话框)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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