• 正则查询文件内容


    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;
    namespace 文件内容搜索
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private string path;
            private string regx;
    
            private void button1_Click(object sender, EventArgs e)
            {
                textBox3.Text = "";
                path = textBox1.Text;
                regx = textBox2.Text;
                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);
                }
                
    
            }
    
            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)
                    {
                        show.Text += filePath + "  行:" + lineNum+"
    ";
                    }
                }
            }
            
        }
    }
    namespace 文件内容搜索
    {
        partial class Form1
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows 窗体设计器生成的代码
    
            /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.label1 = new System.Windows.Forms.Label();
                this.label2 = new System.Windows.Forms.Label();
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.textBox2 = new System.Windows.Forms.TextBox();
                this.textBox3 = new System.Windows.Forms.TextBox();
                this.button1 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // label1
                // 
                this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(32, 26);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(41, 12);
                this.label1.TabIndex = 0;
                this.label1.Text = "根路径";
                // 
                // label2
                // 
                this.label2.AutoSize = true;
                this.label2.Location = new System.Drawing.Point(34, 54);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(41, 12);
                this.label2.TabIndex = 1;
                this.label2.Text = "正则式";
                // 
                // textBox1
                // 
                this.textBox1.Location = new System.Drawing.Point(105, 26);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(206, 21);
                this.textBox1.TabIndex = 2;
                // 
                // textBox2
                // 
                this.textBox2.Location = new System.Drawing.Point(105, 54);
                this.textBox2.Name = "textBox2";
                this.textBox2.Size = new System.Drawing.Size(206, 21);
                this.textBox2.TabIndex = 3;
                // 
                // textBox3
                // 
                this.textBox3.Location = new System.Drawing.Point(36, 110);
                this.textBox3.Multiline = true;
                this.textBox3.Name = "textBox3";
                this.textBox3.ReadOnly = true;
                this.textBox3.ScrollBars = System.Windows.Forms.ScrollBars.Both;
                this.textBox3.ShortcutsEnabled = false;
                this.textBox3.Size = new System.Drawing.Size(275, 113);
                this.textBox3.TabIndex = 4;
                this.textBox3.WordWrap = false;
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(236, 81);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 6;
                this.button1.Text = "搜索";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // Form1
                // 
                this.AcceptButton = this.button1;
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(353, 248);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.textBox3);
                this.Controls.Add(this.textBox2);
                this.Controls.Add(this.textBox1);
                this.Controls.Add(this.label2);
                this.Controls.Add(this.label1);
                this.Name = "Form1";
                this.Text = "文件内容搜索";
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.TextBox textBox2;
            private System.Windows.Forms.TextBox textBox3;
            private System.Windows.Forms.Button button1;
        }
    }

    主要的代码就这些了

  • 相关阅读:
    could not read data from '/Users/lelight/Desktop/ViewControllerLife/ViewControllerLife/Info.plist': The file “Info.plist” couldn’t be opened because there is no such file.
    NSNotification 消息通知的3种方式
    按钮点击播放音效
    字符串变枚举变量
    Flutter的使用教学笔记
    UI控件的位置
    博客园大佬主页跳转
    retain, copy, assign区别
    OC自定义文档头部注释
    OC语言自定义打印
  • 原文地址:https://www.cnblogs.com/mengxingxinqing/p/3202014.html
Copyright © 2020-2023  润新知