• 文件内容查询


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

    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;
                }
            }
            
        }
    }
  • 相关阅读:
    绘画字符串
    方法名,类名与字符串之间的转换
    重写视图的点击区域
    ios7新特性
    常用的sq语句
    基于xmpp的ios聊天客户端(一)
    iOS真机测试,为Provisioning添加设备
    【IOS 开发】Object-C 运算符
    【Android 应用开发】 自定义组件 宽高适配方法, 手势监听器操作组件, 回调接口维护策略, 绘制方法分析 -- 基于 WheelView 组件分析自定义组件
    博客技术资料整理
  • 原文地址:https://www.cnblogs.com/mengxingxinqing/p/3564193.html
Copyright © 2020-2023  润新知