文件内容查询

一个简单的文件内容查询的小工具,没有用多线程,感觉效率非常低,但还是发出来大家一起看看吧!有改进意见了跟我说下,谢谢了!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
namespace 文件内容搜索
{
    public partial class Form1 : Form
    {
        private StringBuilder sb = new StringBuilder();
        public Form1()
        {
            InitializeComponent();
        }
        private string path;
        private string regx;
        private Thread t1;
        private Thread t2;
        private void button1_Click(object sender, EventArgs e)
        {
            textBox3.Text = "";
            path = textBox1.Text;
            regx = textBox2.Text;
            this.backgroundWorker1.RunWorkerAsync();
        }

        private void Thread1()
        {
            Find(path, textBox3);
        }
        private void Thread2()
        {
            FindInFile(new FileInfo(path), textBox3);
        }

        public void Find(string currPath, TextBox show)
        {
            DirectoryInfo dir = new DirectoryInfo(currPath);
            DirectoryInfo[] dirs = dir.GetDirectories();
            FileInfo[] fis = dir.GetFiles();
            foreach(DirectoryInfo d in dirs)
            {
                Find(d.FullName,show);
            }
            foreach (FileInfo f in fis)
            {
                FindInFile(f, show);
            }
        }

        public void FindInFile(FileInfo f, TextBox show)
        {
            Regex re = new Regex(regx, RegexOptions.None);
            string filePath = f.FullName;
            int lineNum = 0;
            StreamReader sr = f.OpenText();
            while (!sr.EndOfStream)
            {
                lineNum++;
                string line = sr.ReadLine();
                if(re.Matches(line).Count>0)
                {
                    sb.Append(filePath + "  行:" + lineNum + "
");
                }
            }
            textBox3.Text += sb.ToString();
        }


        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.button1.Enabled = false;
            if (string.IsNullOrEmpty(path) || string.IsNullOrEmpty(regx))
            {
                MessageBox.Show("不能为空");
                return;
            }
            if (!File.Exists(path) && !Directory.Exists(path))
            {
                MessageBox.Show("文件不存在");
            }
            else if (Directory.Exists(path))
            {
                Find(path, textBox3);
            }
            else
            {
                FindInFile(new FileInfo(path), textBox3);
            }
            this.button1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folders = new FolderBrowserDialog();
            folders.Description = "请选择文件夹";
            folders.ShowNewFolderButton = true;
            folders.RootFolder = Environment.SpecialFolder.DesktopDirectory;
            DialogResult result = folders.ShowDialog();
            if (result == DialogResult.OK)
            {
                string folderName = folders.SelectedPath;
                textBox1.Text = folderName;
            }
        }
        
    }
}
原文地址:https://www.cnblogs.com/mengxingxinqing/p/3564193.html