日期:2014-05-17  浏览次数:20924 次

怎么使多个文本显示在RichTextBox控件中,求解答!
C# code
using (StreamReader sr = new StreamReader("E:\\1.txt", System.Text.Encoding.Default))
                                         {
                                             string TextStr;
                                             TextStr = sr.ReadToEnd().ToString();
                                             sr.Close();
                                             FormFind.txt = TextStr;
                                         } 

FormFind为一个窗体,里面有一个RichTextBox控件,名字为txtReadTxtStr,我在FormFind中定义了txt变量,代码如下:
C# code
 public static string txt ;
private void FormFind_Load(object sender, EventArgs e)
        {
            this.txtReadTxtStr.Text = txt;
        }

假如E盘有30个文本,该怎么把它们显示到RichTextBox中,还请您不吝赐教!

------解决方案--------------------
string[] files = new string[] { @"e:\1.txt", @"e:\2.txt" };
foreach (var file in files)
{
using (StreamReader sr = new StreamReader(file, System.Text.Encoding.Default))
{
string TextStr;
TextStr = sr.ReadToEnd().ToString();
sr.Close();
FormFind.txt += TextStr;

}
------解决方案--------------------
我建议 你把要上传的文本 都放到一个文件夹下面

C# code


 private void button3_Click(object sender, EventArgs e)
        {
            if (FDialog.ShowDialog()==DialogResult.OK)
            {
                string name = FDialog.SelectedPath;
                DirectoryInfo dir = new DirectoryInfo(name);
                DirectoryInfo[] dirs = dir.GetDirectories();
                FileInfo[] flie = dir.GetFiles();

                foreach (FileInfo fi in flie)
                {
                    string val = fi.Extension;
                    if (val == ".txt")
                    {
                        using (StreamReader sr = new StreamReader(fi.FullName, System.Text.Encoding.Default))
                        {
                            string TextStr;
                            TextStr = sr.ReadToEnd().ToString();
                            sr.Close();
                            Form1.txt += TextStr;
                        }  
                    }
                }
            }
        }