阅读文本文件,并保留格式 [英] Read Text File and Keep Formatting

查看:160
本文介绍了阅读文本文件,并保留格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取一个文件,并设置一个RichTextBox等于它的文本,但格式正在消失之后,我这样做。文本文件本身内的ENTERs为不Ri​​chTextBox中可见。这里是code我使用的:

I am attempting to read a file and set the text of a richTextBox equal to it, but the formatting is disappearing after I do so. The "ENTER"s within the text file itself are not visible in the richTextBox. Here is the code I am using:

try
{
    using (StreamReader sr = new StreamReader(directory + filePath))
    {
        while (!sr.EndOfStream)
        {
            initialText += sr.ReadLine();
        }

    }
}

任何帮助将是很大的AP preciated。

Any help would be greatly appreciated.

推荐答案

在使用 sr.ReadLine(),回车从字符串中删除(因为它的行终止)。
试试你读任何行后加入CR:

When you use sr.ReadLine(), carriage return is removed from string (because it's the line terminator).
Try adding a CR after any line you read:

initialText += sr.ReadLine() + Environment.NewLine;

反正你最好使用这个更容易,更快code:

Anyway you'd better to use this easier and faster code:

initialText = File.ReadAllText(directory + filePath)

这篇关于阅读文本文件,并保留格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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