日期:2014-05-18  浏览次数:21197 次

richtextbox怎样读取txt文件或rtf文件
newform.richTextBox1.LoadFile(filename);只能读取rtf格式啊。
有没有即能读取txt文件也能同时读取rtf文件

------解决方案--------------------
读取txt
richTextBox1.LoadFile(@"d:\1.txt", RichTextBoxStreamType.PlainText);
------解决方案--------------------
private void richRead()
{
try
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
if (fs.CanRead)
{
StreamReader sr = new StreamReader(fs, Encoding.Default);
string strline = sr.ReadLine();
StringBuilder sb = new StringBuilder();
while (strline != null)
{
strline = sr.ReadLine();
sb = sb.Append(strline + "\n");
}
sr.Close();
richTextBox1.Text = sb.ToString();
}
}
catch (Exception ex)
{ throw ex; }

}
参考
http://www.cnblogs.com/volnet/archive/2006/11/28/575599.html
------解决方案--------------------
string filename = @"D:\doc\test.txt";
string fileext = new System.IO.FileInfo(filename).Extension;
switch (fileext.ToLower())
{

case ".txt":
this.richTextBox1.LoadFile(filename, RichTextBoxStreamType.PlainText);
break;
case ".rft":
this.richTextBox1.LoadFile(filename, RichTextBoxStreamType.RichText);
break;
}