读取某个文件夹下的所有文件并读取文件中的文本数据

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace ReadFiles
{
    public partial class FormNode : Form
    {
        public FormNode()
        {
            InitializeComponent();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (txtNode.Text.ToString() == "" || txtNode.Text.ToString() == null || txtNode.Text.ToString() == string.Empty)
            {
                MessageBox.Show("请输入要查询的测点!", "测点非空警告!");
                return;
            }

            else if (txtPath.Text.ToString() == "" || txtPath.Text.ToString() == null || txtPath.Text.ToString() == string.Empty)
            {
                MessageBox.Show("请输入文件夹路径!","路径非空警告!");
                return;
            }

            else if (txtNode.Text == "'" || txtNode.Text == "‘")
            {
                MessageBox.Show("含有非法字符请重新输入!","非法字符警告!");
            }

            else
            {
                string path = txtPath.Text.ToString() + "\\";
                FindFile(path);
            }

            #region 测试
            //int count = 0;
            //FileStream fs = new FileStream("", FileMode.Open, FileAccess.Read);
            //StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("GB2312"));
            //string line = sr.ReadLine();
            //for (int i = 0; line != null; i++)
            //{
            //    if (line.Contains("8002") == true)
            //    {
            //        count++;
            //        lisInfo.Items.Add(line);
            //    }
            //    line = sr.ReadLine();
            //}
            //sr.Close();
            //fs.Close();
            #endregion
        }

        public void  FindFile(string dirPath) //参数dirPath为指定的目录
        {
            //在指定目录及子目录下查找文件,在listBox1中列出子目录及文件
            string filename = "";
            DirectoryInfo Dir=new DirectoryInfo(dirPath);
            try
            {
                foreach(DirectoryInfo d in Dir.GetDirectories()) //查找子目录
                {
                    FindFile(Dir+d.ToString()+"\\");
                    lisInfo.Items.Add(Dir+d.ToString()+"\\"); //listBox1中填加目录名
                }
                foreach(FileInfo f in Dir.GetFiles("*.dat")) //查找文件
                {
                    //lisInfo.Items.Add(Dir+f.ToString()); //listBox1中填加文件名
                    //lisInfo.Items.Add(f.ToString()); //listBox1中填加文件名
                    filename = f.ToString();
                    int count = 0;
                    FileStream fs = new FileStream(txtPath.Text + "\\" + filename, FileMode.Open, FileAccess.Read);
                    StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("GB2312"));
                    string line = sr.ReadLine();
                    for (int i = 0; line != null; i++)
                    {
                        if (line.Contains(txtNode.Text.ToString()) == true)
                        {
                            count++;
                            lisInfo.Items.Add(line);
                        }
                        line = sr.ReadLine();
                    }
                    sr.Close();
                    fs.Close();
                }
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message,"异常错误捕获提示!");
            }
        }

        /// <summary>
        /// 打开加载数据窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
       
        private void btnLoad_Click(object sender, EventArgs e)
        {
            LoadFiles ld = new LoadFiles();
            ld.ShowDialog();
        }

        private void btnSound_Click(object sender, EventArgs e)
        {
            sound so = new sound();
            so.ShowDialog();
        }
    }
}

posted on 2010-09-24 13:11  孙州义  阅读(1678)  评论(0编辑  收藏  举报

导航