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
    文章千古事,得失寸心知。


  • 相关阅读:
    Nginx进程信号管理
    Nginx配置缓存服务器
    访问Nginx显示目录
    kubeadm快速安装k8s
    《构建之法》读书笔记(一)
    Android Studio连接SQLite数据库与SQLite Studio实时同步的实现
    关于sqlite数据库与sqlite studio
    AS之去掉顶部标题栏
    今日学习
    AS之AlertDialog使用
  • 原文地址:https://www.cnblogs.com/bimgoo/p/2437683.html
Copyright © 2011-2022 走看看