文件夹中所有文件中的内容搜索源代码
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
MessageBox.Show(exp.Message, "Exception Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
lblStatus.Text = "Status: Search end."; }
// 搜索指定文件夹,并递归搜索其子文件夹 private void SearchDir(string dir) {
// match match = m_RegTxt.Match(line); if (match.Success) {
// add to list ListViewItem lvItem = new ListViewItem(new string[] {
fileName, lineNo.ToString() , line.Substring(match.Index, line.Length-match.Index>51?50:line.Length-match.Index), file.Substring(iRootDirLen) }); listViewSearch.Items.Add(lvItem); }
lblStatus.Text = "Search DIR: " + dir; string[] arrFolders = new string[0]; string[] arrFiles = new string[0];
try {
arrFolders = Directory.GetDirectories(dir); arrFiles Βιβλιοθήκη Baidu Directory.GetFiles(dir); } catch {
MessageBox.Show("Please set the correct Directory Path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return; }
for(int i=0; i"Search DIR: " + dir;
Regex m_RegTxt = null;
// 搜索按钮执行 private void btnSearch_Click(object sender, EventArgs e) {
listViewSearch.Items.Clear();
if ((txtDir.Text.Length < 1) || (txtToSearch.Text.Length < 1)) {
iIndex = file.LastIndexOf('/'); if (iIndex < 0)
iIndex = 0; fileName = file.Substring(iIndex); Match match = null; int iRootDirLen = m_RootDir.Length;
while (line != null) {
MessageBox.Show("Set parameters before search!", "Remind", MessageBoxButtons.OK, MessageBoxIcon.Information);
return; }
if(cboxCase.Checked) m_RegTxt = new Regex(txtToSearch.Text, RegexOptions.Singleline);
else m_RegTxt = new Regex(txtToSearch.Text,
RegexOptions.Singleline|RegexOptions.IgnoreCase);
m_RootDir = txtDir.Text;
try {
SearchDir(txtDir.Text); } catch (Exception exp) {
// search the files for(int i=0; i// 搜索特定的文件 private void SearchFile(string file) { StreamReader srFile = File.OpenText(file); string line = srFile.ReadLine(); int lineNo = 1; string fileName = string.Empty; int iIndex = 0; iIndex = file.LastIndexOf('\\'); if(iIndex < 0)
line = srFile.ReadLine(); ++lineNo; }
srFile.Close(); srFile.Dispose(); srFile = null;
}
}
lblStatus.Text = "Status: Search end."; }
// 搜索指定文件夹,并递归搜索其子文件夹 private void SearchDir(string dir) {
// match match = m_RegTxt.Match(line); if (match.Success) {
// add to list ListViewItem lvItem = new ListViewItem(new string[] {
fileName, lineNo.ToString() , line.Substring(match.Index, line.Length-match.Index>51?50:line.Length-match.Index), file.Substring(iRootDirLen) }); listViewSearch.Items.Add(lvItem); }
lblStatus.Text = "Search DIR: " + dir; string[] arrFolders = new string[0]; string[] arrFiles = new string[0];
try {
arrFolders = Directory.GetDirectories(dir); arrFiles Βιβλιοθήκη Baidu Directory.GetFiles(dir); } catch {
MessageBox.Show("Please set the correct Directory Path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return; }
for(int i=0; i"Search DIR: " + dir;
Regex m_RegTxt = null;
// 搜索按钮执行 private void btnSearch_Click(object sender, EventArgs e) {
listViewSearch.Items.Clear();
if ((txtDir.Text.Length < 1) || (txtToSearch.Text.Length < 1)) {
iIndex = file.LastIndexOf('/'); if (iIndex < 0)
iIndex = 0; fileName = file.Substring(iIndex); Match match = null; int iRootDirLen = m_RootDir.Length;
while (line != null) {
MessageBox.Show("Set parameters before search!", "Remind", MessageBoxButtons.OK, MessageBoxIcon.Information);
return; }
if(cboxCase.Checked) m_RegTxt = new Regex(txtToSearch.Text, RegexOptions.Singleline);
else m_RegTxt = new Regex(txtToSearch.Text,
RegexOptions.Singleline|RegexOptions.IgnoreCase);
m_RootDir = txtDir.Text;
try {
SearchDir(txtDir.Text); } catch (Exception exp) {
// search the files for(int i=0; i// 搜索特定的文件 private void SearchFile(string file) { StreamReader srFile = File.OpenText(file); string line = srFile.ReadLine(); int lineNo = 1; string fileName = string.Empty; int iIndex = 0; iIndex = file.LastIndexOf('\\'); if(iIndex < 0)
line = srFile.ReadLine(); ++lineNo; }
srFile.Close(); srFile.Dispose(); srFile = null;
}