📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace TextFinder
{
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.TreeView tvResult;
private System.Windows.Forms.TextBox tbError;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox tbText;
private System.Windows.Forms.Button btFind;
private System.Windows.Forms.TextBox tbBase;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox tbFilter;
private System.Windows.Forms.Button btSetBaseDir;
private System.Windows.Forms.StatusBar statusBar;
private System.Windows.Forms.StatusBarPanel statusBarPanel1;
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.tvResult = new System.Windows.Forms.TreeView();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.tbError = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.tbText = new System.Windows.Forms.TextBox();
this.btFind = new System.Windows.Forms.Button();
this.tbBase = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.tbFilter = new System.Windows.Forms.TextBox();
this.btSetBaseDir = new System.Windows.Forms.Button();
this.statusBar = new System.Windows.Forms.StatusBar();
this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
this.SuspendLayout();
//
// tvResult
//
this.tvResult.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tvResult.ImageList = this.imageList1;
this.tvResult.Location = new System.Drawing.Point(8, 56);
this.tvResult.Name = "tvResult";
this.tvResult.Size = new System.Drawing.Size(552, 264);
this.tvResult.TabIndex = 6;
this.tvResult.DoubleClick += new System.EventHandler(this.tvResult_DoubleClick);
//
// imageList1
//
this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// tbError
//
this.tbError.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tbError.Cursor = System.Windows.Forms.Cursors.IBeam;
this.tbError.Location = new System.Drawing.Point(8, 320);
this.tbError.Multiline = true;
this.tbError.Name = "tbError";
this.tbError.ReadOnly = true;
this.tbError.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.tbError.Size = new System.Drawing.Size(552, 72);
this.tbError.TabIndex = 8;
this.tbError.Text = "";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(8, 35);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 18);
this.label2.TabIndex = 15;
this.label2.Text = "查找文字:";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(8, 11);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(58, 18);
this.label1.TabIndex = 14;
this.label1.Text = "查找目录";
//
// tbText
//
this.tbText.Location = new System.Drawing.Point(88, 32);
this.tbText.Name = "tbText";
this.tbText.TabIndex = 3;
this.tbText.Text = "";
//
// btFind
//
this.btFind.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btFind.Location = new System.Drawing.Point(392, 8);
this.btFind.Name = "btFind";
this.btFind.TabIndex = 5;
this.btFind.Text = "开始查找";
this.btFind.Click += new System.EventHandler(this.btFind_Click);
//
// tbBase
//
this.tbBase.Location = new System.Drawing.Point(88, 8);
this.tbBase.Name = "tbBase";
this.tbBase.Size = new System.Drawing.Size(224, 22);
this.tbBase.TabIndex = 1;
this.tbBase.Text = "";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(192, 36);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(58, 18);
this.label3.TabIndex = 16;
this.label3.Text = "文件名:";
//
// tbFilter
//
this.tbFilter.Location = new System.Drawing.Point(248, 32);
this.tbFilter.Name = "tbFilter";
this.tbFilter.TabIndex = 4;
this.tbFilter.Text = "*.*";
//
// btSetBaseDir
//
this.btSetBaseDir.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(255)), ((System.Byte)(255)));
this.btSetBaseDir.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btSetBaseDir.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.btSetBaseDir.Location = new System.Drawing.Point(320, 5);
this.btSetBaseDir.Name = "btSetBaseDir";
this.btSetBaseDir.Size = new System.Drawing.Size(24, 24);
this.btSetBaseDir.TabIndex = 2;
this.btSetBaseDir.Text = "...";
this.btSetBaseDir.Click += new System.EventHandler(this.btSetBaseDir_Click);
//
// statusBar
//
this.statusBar.Location = new System.Drawing.Point(0, 390);
this.statusBar.Name = "statusBar";
this.statusBar.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
this.statusBarPanel1});
this.statusBar.ShowPanels = true;
this.statusBar.Size = new System.Drawing.Size(568, 24);
this.statusBar.TabIndex = 19;
//
// statusBarPanel1
//
this.statusBarPanel1.Width = 500;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(7, 15);
this.ClientSize = new System.Drawing.Size(568, 414);
this.Controls.Add(this.statusBar);
this.Controls.Add(this.btSetBaseDir);
this.Controls.Add(this.tbFilter);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.tbText);
this.Controls.Add(this.btFind);
this.Controls.Add(this.tbBase);
this.Controls.Add(this.tbError);
this.Controls.Add(this.tvResult);
this.Font = new System.Drawing.Font("SimSun", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.Name = "Form1";
this.Text = "文字查找";
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.DoEvents();
Application.Run(new Form1());
}
private void tvResult_DoubleClick(object sender, System.EventArgs e)
{
if (tvResult.SelectedNode.ImageIndex==0)
{
MyTreeNode node=tvResult.SelectedNode as MyTreeNode;
System.Diagnostics.Process.Start("Notepad.exe",node.Data);
}
}
private void btFind_Click(object sender, System.EventArgs e)
{
if (tbFilter.Text.Trim()=="")
tbFilter.Text="*.*";
btFind.Enabled=false;
tvResult.Nodes.Clear();
tvResult.BeginUpdate();
MyTreeNode node=new MyTreeNode();
node.Data =tbBase.Text;
node.Text=tbBase.Text;
node.ImageIndex=2;
node.SelectedImageIndex=1;
tvResult.Nodes.Add(node);
FindText(tvResult.Nodes[0] as MyTreeNode,tbBase.Text,tbText.Text.ToLower());
tvResult.EndUpdate();
node.Expand();
statusBarPanel1.Text="Done";
btFind.Enabled=true;
}
public MyTreeNode AddNode(MyTreeNode parent,string name,bool isDir)
{
MyTreeNode node=new MyTreeNode();
node.Data=name;
node.Text=Path.GetFileName(name);
if (isDir)
{
node.ImageIndex=2;
node.SelectedImageIndex=1;
}
else
{
node.SelectedImageIndex=0;
node.ImageIndex=0;
}
parent.Nodes.Add(node);
return node;
}
public void FindText(MyTreeNode parent,string DirName, string TextToFind)
{
//parent.Expand();
string[] sFiles= Directory.GetFiles(DirName,tbFilter.Text);
foreach (string sFile in sFiles)
{
try
{
statusBarPanel1.Text=sFile;
Application.DoEvents();
using (StreamReader sr = new StreamReader(sFile))
{
String line;
while ((line = sr.ReadLine()) != null)
{
line=line.ToLower();
if (line.IndexOf(TextToFind)>0)
{
AddNode(parent,sFile,false);
break;
}
}
sr.Close();
}
}
catch (Exception e)
{
tbError.Text=tbError.Text+'\r'+'\n'+e.Message;
}
}
string[] sDirs=Directory.GetDirectories(DirName);
foreach(string sDir in sDirs)
{
FindText(AddNode(parent,sDir,true),sDir,TextToFind);
}
}
private void btSetBaseDir_Click(object sender, System.EventArgs e)
{
FolderBrowserDialog fb=new FolderBrowserDialog();
fb.ShowNewFolderButton=false;
if (fb.ShowDialog(this)==DialogResult.OK)
{
tbBase.Text=fb.SelectedPath;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -