zoukankan      html  css  js  c++  java
  • 文本搜索器

    本例创建一个文本搜索器,它能在指定目录下指定类型文件中搜索指定的文本,程序界面如下图。

    20120409153008 程序代码如下。

    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();
    		}
    	}
    }
    作者:codee
    文章千古事,得失寸心知。


  • 相关阅读:
    什么是 bean 的自动装配?
    什么是 Spring 的内部 bean?
    什么是 Spring 的 MVC 框架?
    Spring AOP and AspectJ AOP 有什么区别?
    解释 JDBC 抽象和 DAO 模块?
    volatile 类型变量提供什么保证?
    一个 Spring Bean 定义 包含什么?
    什么是 Spring MVC 框架的控制器?
    使用 Spring 访问 Hibernate 的方法有哪些?
    什么是 Callable 和 Future?
  • 原文地址:https://www.cnblogs.com/bimgoo/p/2437683.html
Copyright © 2011-2022 走看看