查找,就是在全文里搜索指定内容,具体方法:
protected void Search()
{
String strDir = txtDir.Text;
Text = "文件内容搜索器 " + strDir;
Cursor = System.Windows.Forms.Cursors.WaitCursor;
//File Extension
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)
{
//Add the Line to Results string
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";
}
//catch(AccessException)
//{
// strResults += "\r\n" + (string)enm.Current + ": Access Exception\r\n";
//}
}
txtResults.Text = strResults;
Cursor = System.Windows.Forms.Cursors.Arrow;
}
protected void Search()
{
String strDir = txtDir.Text;
Text = "文件内容搜索器 " + strDir;
Cursor = System.Windows.Forms.Cursors.WaitCursor;
//File Extension
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)
{
//Add the Line to Results string
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";
}
//catch(AccessException)
//{
// strResults += "\r\n" + (string)enm.Current + ": Access Exception\r\n";
//}
}
txtResults.Text = strResults;
Cursor = System.Windows.Forms.Cursors.Arrow;
}
{
Search();
}加个按钮触发就可以了吧。