文本搜索器
2012-04-08 15:33 精诚所至 金石为开 阅读(500) 评论(0) 编辑 收藏 举报本例创建一个文本搜索器,它能在指定目录下指定类型文件中搜索指定的文本,程序界面如下图。
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.Security; namespace eg47_findApp { public partial class MainForm : Form { ArrayList m_arrFiles=new ArrayList(); public MainForm() { InitializeComponent(); } void BtnBrowseClick(object sender, EventArgs e) { OpenFileDialog fdlg=new OpenFileDialog(); fdlg.Title="Select a file"; fdlg.Filter="All filter(*.*)|*.*"; if(fdlg.ShowDialog()==DialogResult.OK) { string strPath=fdlg.FileName; string strExt; FileInfo f=new FileInfo(strPath); txtDir.Text=f.DirectoryName; strExt=f.Extension; if(strExt!="") strExt=strExt.Substring(1); txtFiles.Text=strExt; } } void BtnSearchClick(object sender, EventArgs e) { Search(); } protected void Search() { string strDir=txtDir.Text; DirectoryInfo dir=new DirectoryInfo(strDir); if(!dir.Exists) MessageBox.Show("Directory doesn't exist!","Win Grep Erro"); else { Text="文件内内容搜索器"+strDir; Cursor=System.Windows.Forms.Cursor.Current; string strExt=txtFiles.Text; if(strExt!="") if(strExt.StartsWith(".")) { strExt=strExt.Substring(1); } m_arrFiles.Clear(); GetFiles(strDir,strExt,ckInclude.Checked); string strSearch=txtSearchtext.Text; string strResults=""; string strLine; int iLine; IEnumerator enm=m_arrFiles.GetEnumerator(); while(enm.MoveNext()) { try { StreamReader sr=File.OpenText((string)enm.Current); iLine=0; string strResultsInThisFile=""; while((strLine=sr.ReadLine())!=null) { iLine++; if(strLine.IndexOf(strSearch)!=-1) { strResultsInThisFile+=""+iLine+":"+strLine+"\r\n"; } } sr.Close(); if(chkNotListAll.Checked) { if(strResultsInThisFile.Length>0) { strResults+="\r\n"+(string)enm.Current+":\r\n"; strResults+=strResultsInThisFile; } } else { strResults+="\r\n"+(string)enm.Current+"\r\n"; strResults+=strResultsInThisFile; } } catch(SecurityException) { strResults+="\r\n|"+(string)enm.Current+":Security exception\r\n\r\n"; } } txtResults.Text=strResults; Cursor=System.Windows.Forms.Cursors.Arrow; } } protected void GetFiles(string strDir,string strExt,bool bRecursive) { DirectoryInfo dir=new DirectoryInfo(strDir); FileInfo[]fileList=dir.GetFiles("*."+strExt); for(int i=0;i<fileList.Length;i++) { if(fileList[i].Exists) { m_arrFiles.Add(strDir+"\\"+fileList[i].Name); } } if(bRecursive==true) { DirectoryInfo[]dirList=dir.GetDirectories(); for(int i=0;i<dirList.Length;i++) { GetFiles(strDir+"\\"+dirList[i].Name,strExt,bRecursive); } } } void TxtFilesKeyDown(object sender, KeyEventArgs e) { if(e.KeyCode==Keys.Enter&&btnSearch.Enabled==true) { Search(); } } void TxtDirKeyDown(object sender, KeyEventArgs e) { if(e.KeyCode==Keys.Enter&&btnSearch.Enabled==true) { Search(); } } void TxtSearchtextKeyDown(object sender, KeyEventArgs e) { if(e.KeyCode==Keys.Enter&&btnSearch.Enabled==true) { Search(); } } void TxtDirTextChanged(object sender, EventArgs e) { VerifySearchBtn(); } protected void VerifySearchBtn() { if(txtDir.Text!=""&&txtSearchtext.Text!="") { btnSearch.Enabled=true; } else btnSearch.Enabled=false; } void TxtSearchtextTextChanged(object sender, EventArgs e) { VerifySearchBtn(); } } }
![](https://img2024.cnblogs.com/blog/35695/202502/35695-20250207193705881-1356327967.jpg)