本文介绍了如何每行写一个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有一个富文本框,其内容应写入文件,但是每行一行,就像在富文本框中显示的一样.
我该怎么办?
我想保留格式,这非常重要.
Hello everyone,
I have a rich text box of which the content should be written to a file, but line per line, just like its displayed in the rich text box.
how do I do this?
i want to retain the formatting, that''s very important.
推荐答案
public static void Main()
{
string path = @"c:\test.txt";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("This");
sw.WriteLine("code");
sw.WriteLine("will");
sw.WriteLine("write");
sw.WriteLine("text");
sw.WriteLine("per");
sw.WriteLine("line");
}
}
}
因此,您可以按如下所示分别编写RichTextBox控件的每一行
So you can able to write each line each of RichTextBox control as follows
string path = @"c:\test.rtf";
using (StreamWriter sw = File.CreateText(path))
{
foreach (string line in richTextBox1.Lines)
{
sw.WriteLine(line);
}
}
这篇关于如何每行写一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!