如何每行写一个文件? [英] How to write to a file line per line?
本文介绍了如何每行写一个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有一个富文本框,其内容应写入文件,但是每行一行,就像在富文本框中显示的一样.
我该怎么办?
我想保留格式,这非常重要.
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.
推荐答案
[基于您更新的问题的替代答案.]
如果要将其另存为RTF,请使用RichTextBox.SaveFile
的重载.
请参阅 http://msdn.microsoft.com/en-us/library/ch779a3b.aspx [ ^ ]
[Alternate answer based on your updated question.]
If you want to save it as RTF, use an overload ofRichTextBox.SaveFile
.
See http://msdn.microsoft.com/en-us/library/ch779a3b.aspx[^]
以下代码段可帮助您每行写一个文本.
注意我假设您要在文本文件上写.
The following code snippet helps you to write a text per a single line.
Note I am assuming that you want to write on a text file.
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);
}
}
您可以使用File.WriteAllLines
或仅使用File.WriteAllText
.哪个更适合您.
You can use eitherFile.WriteAllLines
or justFile.WriteAllText
. Whichever works better for you.
这篇关于如何每行写一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文